【uniapp】小程序开发7:自定义组件、自动注册组件

一、自定义轮播图组件、自动注册

以首页轮播图组件为例。

1、创建组件文件src/components/my-swipper.vue

代码如下:

html 复制代码
<template>
	<view>
		<view class="uni-margin-wrap">
			<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
				:duration="duration">
				<swiper-item>
					<view class="swiper-item uni-bg-red">A</view>
				</swiper-item>
				<swiper-item>
					<view class="swiper-item uni-bg-green">B</view>
				</swiper-item>
				<swiper-item>
					<view class="swiper-item uni-bg-blue">C</view>
				</swiper-item>
			</swiper>
		</view>

	</view>
</template>

<script lang="ts">
export default {
    data() {
        return {
            background: ['color1', 'color2', 'color3'],
            indicatorDots: true,
            autoplay: true,
            interval: 2000,
            duration: 500
        }
    },
    methods: {
        changeIndicatorDots(e) {
            this.indicatorDots = !this.indicatorDots
        },
        changeAutoplay(e) {
            this.autoplay = !this.autoplay
        },
        intervalChange(e) {
            this.interval = e.target.value
        },
        durationChange(e) {
            this.duration = e.target.value
        }
    }
}
</script>

<style>
	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
	}
	.swiper {
		height: 300rpx;
	}
	.swiper-item {
		display: block;
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
    color: white;
	}
  .uni-bg-red{
    background-color: red;
  }
  .uni-bg-green{
    background-color: green;
  }
  .uni-bg-blue{
    background-color: blue;
  }
</style>

2、在pages.json中配置自动注册,添加配置代码"^my-(.*)": "@/components/my-$1.vue"

json 复制代码
{
	"easycom": {
		"autoscan": true,
		"custom": {
			// uni-ui 规则如下配置
			"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue",
			// 自定义组件,配置自动导入
			"^my-(.*)": "@/components/my-$1.vue"
		}
	},
    // ... //	
}

3、在页面中直接使用<my-navbar/>

不用再手动import

html 复制代码
<template>
  <my-navbar/>
  <view class="content">
    <image mode="aspectFill" class="logo" src="/static/logo.png" />
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>
  </view>
</template>

4、配置ts类型提示

在组件目录components创建文件components.d.ts,内容如下,

ts 复制代码
import MySwipper from "./my-swipper.vue";
declare module '@vue/runtime-core' {
    export interface GlobalComponents {
        MySwipper: typeof MySwipper,
    }
}

二、增加轮播图指示点组件

uni-swiper-dot组件在swiper 基础上面增加了丰富的指示点样式。

UniHelper为我们提供了各种组件的类型声明,可以直接使用。如下面change事件中的ev参数 ev: UniHelper.SwiperOnChangeEvent

使用轮播图指示点组件需要把swiper组件的默认指示点(indicator-dots)设置为false,否则会出现两层指示点。

核心参数

  • mode:指示点的类型,可选值:default 、round 、nav 、 indexes
  • current:当前指示点索引,必须是通过 swiper 的 change 事件获取到的 e.detail.current
  • dotsStyles:指示点样式
  • info:轮播图的数据,通过数组长度决定指示点个数
  • field:mode 为 nav 时,显示的内容字段(mode = nav 时必填)
html 复制代码
<template>
	<view>
		<view class="uni-margin-wrap">
			<uni-swiper-dot class="uni-swiper-dot-box" @clickItem=clickItem :info="info" :current="current" :mode="mode"
				:dots-styles="dotsStyles" field="content">
				<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
					:current="swiperDotIndex" :duration="duration" @change="onchange">
					<swiper-item v-for="(item, index) in info">
						<view :class="'swiper-item ' + item.colorClass">{{ item.content }}</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
	</view>
</template>
js 复制代码
<script lang="ts">
export default {
	data() {
		return {
			indicatorDots: false,
			autoplay: true,
			interval: 2000,
			duration: 500,
			info: [{
				colorClass: 'uni-bg-red',
				content: '内容 A'
			},
			{
				colorClass: 'uni-bg-green',
				content: '内容 B'
			},
			{
				colorClass: 'uni-bg-blue',
				content: '内容 C'
			}
			],
			modeIndex: -1,
			styleIndex: -1,
			current: 0,
			mode: 'default',//default, dot, round, nav, indexes
			dotsStyles: {
				backgroundColor: 'rgba(83, 200, 249,0.3)',
				border: '1px rgba(83, 200, 249,0.3) solid',
				color: '#fff',
				selectedBackgroundColor: 'rgba(83, 200, 249,0.9)',
				selectedBorder: '1px rgba(83, 200, 249,0.9) solid'
			},
			swiperDotIndex: 0
		}
	},
	methods: {
		onchange(ev: UniHelper.SwiperOnChangeEvent) {
			this.current = ev.detail.current
		},
		clickItem(e: number) {
			this.swiperDotIndex = e
		}
	}
}
</script>

参考

相关推荐
Momo__38 分钟前
Vite 8.1 打包开发模式——10000 组件冷启动 15 倍,"No Bundle Dev" 七年后被自己终结
前端·vue.js·vite
YHL41 分钟前
🤖 Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·javascript·人工智能
Monki42 分钟前
AI 规范式 UI 设计,一键批量出页,高效落地不翻车
前端
山河木马43 分钟前
渲染管线-计算得到gl_FragColor(片元着色器)之后续GPU流程
前端·webgl·计算机图形学
不想说话的麋鹿1 小时前
「项目实战」我用 Codex + GPT-5.5 + Trellis "氛围编程",独立做了一个情侣厨房小程序
前端·agent·全栈
接着奏乐接着舞。2 小时前
【2026年7月最新】69道RAG面试题
前端·人工智能·后端·aigc·embedding·rag
以和为贵2 小时前
前端也能搞懂 Agent:从 Function Calling 到自主编排
前端·人工智能·架构
风止何安啊2 小时前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
J船长2 小时前
扫盲烂笔头:A 记录、CNAME 的作用, Nginx的用途,为啥叫反向代理
前端
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(二十六):icon 组件封装
前端·vue.js·typescript