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

效果如下

相关推荐
Hyyy6 小时前
普通前端续命周报——第1周
前端·javascript
KaMeidebaby7 小时前
卡梅德生物技术快报|抗独特型抗体开发:半抗原检测技术瓶颈拆解,抗独特型抗体开发工程化实践
前端·数据库·人工智能·其他·百度·新浪微博
2501_940041747 小时前
纯前端创意交互:五款全新实用工具与视觉应用生成指南
前端·交互
刀法如飞7 小时前
《道德经》简单解说版-第 2 章:天下皆知美之为美
前端·后端·面试
GISer_Jing9 小时前
Three.JS渲染架构解读
java·javascript·架构
发现一只大呆瓜9 小时前
超全 Vite 性能优化指南:网络、资源、预渲染三维落地方案
前端·面试·vite
IT_陈寒9 小时前
Vue的computed属性怎么突然不更新了?
前端·人工智能·后端
时寒的笔记9 小时前
day13~14核心案例某采招网
开发语言·javascript·ecmascript
智商不够_熬夜来凑10 小时前
【Picker】单选多选
前端·javascript·vue.js