首先说明:首页只负责布局,局部样式功能全部放在子组件,做到真正的模块化。分别引入子组件到首页,相应位置即可。子组件的数据可以通过首页执行ajax请求服务器,数据发送给子组件即可。
主页面-首页
保存文件路径 pages/index/index.vue
<template>
<view>
<page-head :title="title"></page-head>
<view class="page-body">
<header></header>
<scroll-view class="main-content" style="flex: 1;" @scroll="scroll" @scrolltolower="scrolltolower" scroll-y>
//引入广告子组件
<view class="" style="height: 300upx;width: 100vw;">
<big-ad ref="bigAd" ></big-ad>
</view>
//引入列表页子组件
<view class="uni-flex-item" >
<product-list ref="productList" ></product-list>
</view>
<view v-if="loading" class="load-more">加载中……</view>
</scroll-view>
<footer class="" >
//引入底部导航子组件
<bottom-nav ref="bottomNav" ></bottom-nav>
</footer>
</view>
</view>
</template>
<script>
import productList from '../../components/template/product/list.vue';
import bottomNav from '../../components/template/nav/bottom.vue';
import bigAd from '../../components/template/swiper/big-ad.vue';
export default {
name: "index/index",
data() {
return {
searchInput:'',
index:0,
loading:true,//下拉加载开启
number:1,//每页请求数量
items:[
{
fn:this.tijiao,
msg:0,
name:'首页',
ico:'icon-fangzi',
url:'/pages/index/index'
},{
fn:this.tijiao,
msg:0,
name:'店铺页',
ico:'icon-dianpu',
url:'/pages/goods/category'
},
{
fn:this.tijiao,
msg:230,
name:'购物车',
ico:'icon-gouwuche',
url:'/pages/goods/cart'
},
{
fn:this.tijiao,
msg:0,
name:'我的',
ico:'icon-wode',
url:'/pages/user/index'
}
],
}
},
methods: {
scrolltolower(e){
// 如果下来触发状态未关闭 将会触发请求服务器
if(this.loading){
setTimeout(()=> {
setTimeout(()=> {
this.loadData();
}, 1000);
}, 1000);
}
console.log("底部")
// 滚到底部或者右边触发
},
scroll(e){
// 滚动触发
// console.log(e)
},
loadData(action = 'add') {
const data = [
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product1.jpg',
title: 'Apple iPhone X 256GB 深空灰色 移动联通电信4G手机',
originalPrice: 9999,
favourPrice: 8888,
tip: '自营'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product2.jpg',
title: 'Apple iPad 平板电脑 2018年新款9.7英寸',
originalPrice: 3499,
favourPrice: 3399,
tip: '优惠'
}
];
if (action === 'refresh') {
this.$refs.productList.productList = [];
}
if(data.length<this.number){
// 如果服务器端数据少于20条关闭下拉触发
this.loading=false;
}else{
data.forEach(item => {
this.$refs.productList.productList.push(item);
});
}
},
tijiao(item){
console.log(item)
uni.navigateTo({
url: item.url
});
}
},
mounted: function (){
// el渲染完成触发
this.$nextTick(function () {
// this.$refs.bottomNav.index=2;
// 替换底部nav 内容
this.$refs.bottomNav.items=this.items;
// console.log(this.items)
// console.log(this.$refs.bottomNav);//
})
// this.$refs.bottomNav.index=2;
},components: {
bigAd,bottomNav,productList
},
}
</script>
<style>
.load-more{
height: 3em;
}
.page-body{
justify-content:space-between;
flex-direction: column;
align-content: ;
height: 100vh;
box-sizing:border-box;
display: flex;
}
header{
}
.main-content{
margin-bottom: 4.2em;
text-align: center;
width: 100%;
flex:1;
overflow-y:auto;
box-sizing:border-box;
}
footer{
left: 0;
right: 0;
position:fixed;
bottom: 0;
height: 110upx;
font-size:1em ;
background:red;
bottom: 0;
}
</style>
子组件1:大屏广告
列表页文件路径位置 import bigAd from '../../components/template/swiper/big-ad.vue';
<template>
<!-- <page-head :title="title"></page-head> -->
<view class="page-section">
<view class="swiper-item-badge" v-if="items.length">
<view :style="index==idx?'background:#fff':''" @tap="index=idx" v-for="(item,idx) in items" :key="idx" >
{{idx+1}}
</view>
</view>
<swiper class="swiper" :indicator-dots="indicatorDots" :current="index" @change="change" :autoplay="autoplay" :interval="interval" :duration="duration">
<swiper-item v-for="(item,idx) in items" :key="idx" class="swiper-item ">
<!-- <view class="swiper-item " v-for="(item,index) in items" :key="index"> -->
<view class="swiper-item-title">{{item.title}}</view>
<image :src="item.image" mode="aspectFill"></image>
<!-- </view> -->
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
data() {
return {
// height:'300upx',
// title: 'swiper',
items:[
{
title:"明天的生活会更好,大家的努力都值得,明天的生活会更好,大家的努力都值得",
image:"https://p.pstatp.com/weili/bl/57413396244601585.jpg",
},{
title:"明天的生活会更好,大家的努力都值得,明天的生活会更好,大家的努力都值得",
image:"https://p.pstatp.com/weili/bl/57421573862329129.jpg",
},{
title:"明天的生活会更好,大家的努力都值得,明天的生活会更好,大家的努力都值得",
image:"https://p.pstatp.com/weili/bl/57421573862329129.jpg",
}
],
index:0,//当前显示索引
indicatorDots: false,//显示指示点
autoplay: true,//自动轮播
interval: 2000,//每次轮播间隔时间
duration: 1000,//轮播进行速度
}
},
methods: {
change(e){
// 监听轮播变化
// console.log(e)
this.index=e.detail.current;
},
setIndicatorDots(e) {
// 开启/关闭指示点
this.indicatorDots = !this.indicatorDots
},
setAutoplay(e) {
// 开启/关闭轮播自动
this.autoplay = !this.autoplay
},
setInterval(num) {
// 设置轮播间隔时间
this.interval = num;
},
setDuration(num) {
// 设置轮播进行速度
this.duration = num
}
}
}
</script>
<style>
.page-section{
height: 100%;
/* 调用页面外层div要设置高度 */
/* height: 500upx; */
box-sizing: border-box;
width: 100%;
min-height: 50upx;
text-align: center;
position:relative;
}
.swiper{
height: 100%;
width: 100%;
}
/* .swiper-item {
text-align: center;
} */
.swiper-item-title{
opacity:0.5;
background: #2F2F2F;
line-height: 1em;
box-sizing: border-box;
width: 100%;
padding: 0.5em;
bottom: 0;
position:absolute;
overflow: hidden;/*超出部分隐藏*/
white-space: nowrap;/*不换行*/
text-overflow:ellipsis;/*超出部分文字以...显示*/
}
.swiper-item-badge{
top: 0.2em;
display: flex;
right: 1em;
z-index: 1;
position:absolute;
}
.swiper-item-badge view{
line-height: 1em;
margin: 0 0.1em;
/* text-align: center; */
padding:0.2em;
opacity:0.5;
background: #000;
}
.swiper-item image{
min-height: 100%;
min-width: 100%;
}
</style>
子组件2-列表页
列表页文件路径位置 import productList from '../../components/template/product/list.vue';
<template>
<view class="uni-product-list">
<view class="uni-product " hover-class="uni-list-cell-hover" v-for="(product,index) in productList" :key="index">
<view class="image-view">
<image lazy-load v-if="renderImage" class="uni-product-image" :src="product.image"></image>
</view>
<view class="uni-product-title">{{product.title}}</view>
<view class="uni-product-price">
<text class="uni-product-price-favour">¥{{product.originalPrice}}</text>
<text class="uni-product-price-original">¥{{product.favourPrice}}</text>
<text class="uni-product-tip">{{product.tip}}</text>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
productList: [
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product1.jpg',
title: 'Apple iPhone X 256GB 深空灰色 移动联通电信4G手机',
originalPrice: 9999,
favourPrice: 8888,
tip: '自营'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product2.jpg',
title: 'Apple iPad 平板电脑 2018年新款9.7英寸',
originalPrice: 3499,
favourPrice: 3399,
tip: '优惠'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product3.jpg',
title: 'Apple MacBook Pro 13.3英寸笔记本电脑(2017款Core i5处理器/8GB内存/256GB硬盘 MupxT2CH/A)',
originalPrice: 12999,
favourPrice: 10688,
tip: '秒杀'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product4.jpg',
title: 'Kindle Paperwhite电纸书阅读器 电子书墨水屏 6英寸wifi 黑色',
originalPrice: 999,
favourPrice: 958,
tip: '秒杀'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product5.jpg',
title: '微软(Microsoft)新Surface Pro 二合一平板电脑笔记本 12.3英寸(i5 8G内存 256G存储)',
originalPrice: 8888,
favourPrice: 8288,
tip: '优惠'
},
{
image: 'https://img-cdn-qiniu.dcloud.net.cn/uploads/example/product6.jpg',
title: 'Apple Watch Series 3智能手表(GPS款 42毫米 深空灰色铝金属表壳 黑色运动型表带 MQL12CH/A)',
originalPrice: 2899,
favourPrice: 2799,
tip: '自营'
},
],
};
},
methods: {
},
};
</script>
<style>
.uni-product-list{
display:flex;
justify-content:space-around;
height: 100%;
width: 100%;
box-sizing:border-box;
}
.uni-product{
width: 100%;
flex: 1;
}
</style>
子组件三:底部导航
文件路径位置 import productList from '../../components/template/nav/bottom.vue';
<template>
<view class="bottom-nav">
<view class="uni-flex-item" v-for="(item,idx) in items" :key="idx" @tap="index=idx,(item.fn)(item)" :class="index==idx?'uni-badge-primary uni-badge-inverted':''">
<view class="uni-flex-item uni-column">
<view class="nav-icon uni-flex-item">
<view class="nav-msg" >
<view class="uni-badge uni-badge-danger" v-if="item.msg">{{item.msg}}</view>
</view>
<icon class="uni-flex-item iconfont" :class="item.ico" >
</icon>
</view>
<view class="uni-flex-item nav-title">
{{item.name}}
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
index:0,
items:[
{
fn:this.tijiao,//自定义点击触发函数,并且携带被触发的内容数据
msg:0,//未读消息数量
name:'首页',//标题
ico:'icon-fangzi',// iconfont字体图标
url:''//跳转url
},{
fn:this.tijiao,
msg:1,
name:'订单',
ico:'icon-dingdanjihe',
url:'/pages/goods/category'
},{
fn:this.tijiao,
msg:0,
name:'店铺',
ico:'icon-dianpu',
url:'/pages/goods/category'
},{
fn:this.tijiao,
msg:0,
name:'我的',
ico:'icon-wode',
url:'/pages/goods/category'
}]
};
},mounted(){
console.log("mounted")
// console.log(this.$refs.bottomnav)
}
}
</script>
<style>
.bottom-nav{
width: 100%;
height: 100%;
right: 0;
position:fixed;
bottom: 0; */
background:hsl(180,0%,90%);
font-size: 1em;
display: flex;
flex-direction: row;
text-align: center;
}
.bottom-nav .nav-icon{
box-sizing: border-box;
}
.bottom-nav .nav-msg{
margin-left:2em;
height: 10upx;
}
.bottom-nav .nav-msg .uni-badge{
padding:1upx 8upx;
font-size:1em ;
}
.bottom-nav .nav-title{
height: 1.8em;
font-size:0.8em ;
}
.bottom-nav .nav-icon{
line-height: 1em;
font-size: 2em;
}
</style>