带你用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>

效果如下

相关推荐
^Rocky4 分钟前
uniapp 对接腾讯云IM群组成员管理(增删改查)
uni-app·腾讯云
程序员马晓博5 分钟前
深入聊聊Qwen3的混合推理:全球唯三,开源唯一
前端·后端
懋学的前端攻城狮6 分钟前
Vue源码解析-01:从创建到挂载的完整流程
前端·vue.js·源码
Allen Bright11 分钟前
【CSS-5】掌握CSS文本样式:从基础到高级技巧
前端·css
贩卖纯净水.18 分钟前
Webpack常见的插件和模式
前端·webpack·node.js
brzhang26 分钟前
Flutter 调用原生代码,看这篇就够了:从零教你搭起通信的桥
前端·后端·架构
程序员小张丶27 分钟前
React Native在HarmonyOS 5.0阅读类应用开发中的实践
javascript·react native·react.js·阅读·harmonyos5.0
EndingCoder27 分钟前
React Native 是什么?为什么学它?
javascript·react native·react.js
袁煦丞27 分钟前
知识管理的六边形战士Trilium Notes:cpolar内网穿透实验室第520个成功挑战
前端·程序员·远程工作
失败又激情的man36 分钟前
python爬虫之数据存储
前端·数据库·python