学习地址
零基础玩转微信小程序:黑马程序员前端微信小程序开发教程,微信小程序从基础到发布全流程_企业级商城实战(含uni-app项目多端部署)
uniapp - 黑马优购 笔记地址
知识点
1 创建组件
2 使用组件
3 设置传入参数 与data() 同级 props 中
goods: {
type: Object, //类型
default: {} // 默认值
}
4 图片默认值
<image class="itemImage" :src="goods.goods_small_logo || defaultPic"></image>
5 配置 上拉加载 ,和 底部刷新
图中包含 ,底部,顶部 的相关操作逻辑
6 其他知识点 ,加深记忆
源码
good_list
<template>
<view>
<view v-for="(item , index) in goodsList" :key="index" @click="itemClick(item)">
<my-goods :goods="item"></my-goods>
</view>
</view>
</template>
<script>
export default {
data() {
return {
queryObj: {
query: '',
cid: '',
pagenum: 1,
pagesize: 10
},
goodsList: [],
total: 0,
isLoading : false
}
},
onLoad(options) {
this.queryObj.query = options.query || ''
this.queryObj.cid = options.cid || ''
this.getGoodsList()
},
methods: {
async getGoodsList(cb) {
this.isLoading = true
const {
data: res
} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
this.isLoading = false
cb && cb()
if (res.meta.status !== 200) return uni.$showMsg()
this.goodsList = [...this.goodsList , ...res.message.goods]
this.total = res.message.total
console.log(this.goodsList)
}
,
itemClick(item){
uni.navigateTo({
url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
})
}
}
,
onReachBottom(){
if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')
if(this.isLoading) return
this.queryObj.pagenum +=1
this.getGoodsList()
},
onPullDownRefresh(){
if(this.isLoading) return
this.queryObj.pagenum = 1
this.total = 0
this.goodsList = []
this.getGoodsList(()=>{
uni.stopPullDownRefresh()
})
}
}
</script>
<style lang="scss">
</style>
my-goods
<template>
<view>
<view class="itemLayout">
<image class="itemImage" :src="goods.goods_small_logo || defaultPic"></image>
<view class="itemRightLayout">
<view class="itemTitle">
{{goods.goods_name }}
</view>
<view class="itemPrice"> ¥{{ goods.goods_price | tofixedNew }} </view>
</view>
</view>
</view>
</template>
<script>
export default {
name: "my_goods",
data() {
return {
defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
}
},
props: {
goods: {
type: Object,
default: {}
}
},
methods:{
},
filters:{
tofixedNew(num){
return Number(num)+1
}
},
}
</script>
<style lang="scss">
.itemLayout {
display: flex;
height: 80px;
align-items: center;
justify-content: space-between;
padding: 0 20rpx;
border-bottom: 1px solid #efefef;
margin-bottom: 20rpx;
.itemImage {
width: 80px;
height: 60px;
}
.itemRightLayout {
flex: 1;
display: flex;
height: 60px;
flex-direction: column;
font-size: 24rpx;
margin-left: 16rpx;
margin-right: 16rpx;
justify-content: space-between;
.itemPrice {
color: #C00000;
}
}
}
</style>