带你用uniapp从零开发一个仿小米商场_9. 轮播图组件封装及使用

导航栏有了,接下来就是轮播图了,轮播图如下,

因为uniapp 官方自己有轮播图,所以这里就不自己写了,直接使用uniapp的轮播图二次开发就好

uniapp的轮播图组件叫swiper ,感兴趣的朋友可以点击链接,直接去看官方文档,也可以看我这里实操

用hbuilderX编译uniapp的代码有一个好处,如下,这个快捷输入

编写uniapp的内置组件时,可以输入u+组件名称,能直接输出内置好的模板

然后直接将images放进去就好

效果如下

能进行展示了,那就开始封装

在根路径下新建components目录,然后右键新建组件,

接下来就能在swiper-imgs组件中将刚刚写好的轮播图复制过来了

并进行一些修改,

  1. 将图片的src属性设置为动态属性,并且声明一个props对象,接收一个swdata数组
  2. swdata数组用来接收父组件传递过来的值
  3. 然后使用template包裹swiper-item标签,并循环swdata数组
  4. 定一个tapEvent事件,作为后续有需求时添加

然后就可以在页面上引用了

import swiperImgs from '@/components/swiper-imgs/swiper-imgs.vue'

页面代码

html 复制代码
<template>
	<view class="">
		
		<!-- <swiper indicator-dots autoplay :interval="3000" :duration="1000" circular>
			<swiper-item>
				<view class="swiper-item">
					<image src="/static/images/demo/demo4.jpg" lazy-load style="height: 350rpx;"></image>
				</view>
			</swiper-item>
		</swiper> -->
		<swiper-imgs :swdata="srcData"></swiper-imgs>
	</view>
	
	
</template>

<script>
	import swiperImgs from '@/components/swiper-imgs/swiper-imgs.vue'
	export default {
		data() {
			return {
				title: 'Hello',
				logoShow:false,
				srcData:[
					{src:'/static/images/demo/demo4.jpg'},
					{src:'/static/images/demo/demo8.jpg'},
					{src:'/static/images/demo/search-banner.png'},
					{src:'/static/images/demo/demo4.jpg'},
					{src:'/static/images/demo/search-banner.png'},
				]
			}
		},
		onLoad() {

		},
		methods: {
			showLogo(){
				this.logoShow=!this.logoShow
			}
		},
		components:{
			swiperImgs
		}
	}
</script>

<style>
	
</style>

效果如下

相关推荐
我命由我123454 小时前
CesiumJS 笔记 - 获取容器中心点、Cartesian3 clone 方法、修改 Cartesian3 对象的高度
前端·javascript·css·前端框架·html·html5·js
AI_AGENT_DEV_AI4 小时前
原生 APP 开发的开发优点
uni-app
Hopebearer_5 小时前
页面突然只剩 DOM?一次静态资源版本错配排查
前端·部署
抱抱宝5 小时前
Agent-study项目教程(03):手写 Mini-ReAct Agent(不依赖框架)
javascript·人工智能·gpt·react.js·prompt·agent
东风破_5 小时前
ESLint 是什么?为什么你的项目需要它?
前端·后端·代码规范
用户938515635075 小时前
Next.js 笔记系统(二):Redis 数据服务与侧边栏组件拆分实战
javascript·全栈
圣殿骑士-Khtangc7 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
BigTopOne7 小时前
【ijkplayer】 硬解码流程
前端
kyriewen8 小时前
DeepSeek Harness开源第一天我就上手了——和Claude Code的差距比想象中大
前端·ai编程·deepseek
捡田螺的小男孩8 小时前
什么是 Skill?手把手带你写一个简单有用的 Skill!
前端·后端·程序员