组件goods-list 代码如下:

    1. <template>
    2. <view class='goods_list'>
    3. <view class='goods_item' v-for="item in goods" :key="item.id" @click="navigator(item.id)">
    4. <image :src='item.img_url'></image>
    5. <view class='price'>
    6. <text>¥{{item.sell_price}}</text>
    7. <text>¥{{item.market_price}}</text>
    8. </view>
    9. <view class="name">
    10. {{item.title}}
    11. </view>
    12. </view>
    13. </view>
    14. </template>
    15. <script>
    16. export default {
    17. props:['goods'], //goods传递过来的数据
    18. methods:{
    19. navigator(id){
    20. this.$emit('goodsItemClick',id)
    21. }
    22. }
    23. }
    24. </script>
    25. <style lang='scss'>
    26. .goods_list {
    27. padding: 0 15rpx;
    28. display: flex;
    29. flex-wrap: wrap;
    30. justify-content: space-between;
    31. .goods_item {
    32. background: #fff;
    33. width: 355rpx;
    34. margin: 10rpx 0;
    35. padding: 15rpx;
    36. box-sizing: border-box; //不会撑开盒子了
    37. image {
    38. width: 95%;
    39. height: 200px;
    40. display: block;
    41. margin: 0 auto; //上面下面实现居中
    42. }
    43. .price {
    44. color: $shop-color;
    45. font-size: 36rpx;
    46. margin: 5rpx 0;
    47. text:nth-child(2) {
    48. color: #CCC;
    49. font-size: 28rpx;
    50. margin-left: 17rpx;
    51. text-decoration: line-through;
    52. }
    53. }
    54. .name {
    55. font-size: 22rpx;
    56. line-height: 40rpx;
    57. }
    58. }
    59. }
    60. </style>

    上面的代码需要注意的是:在每条数据创建点击事件,然后用$emit传递给父组件。
    父组件用传递过来的函数名称进行接受,比如这里使用的是 goodsItemClick 所以两边都需要用一致的函数名称。

    这个时候在商品列表也是不起作用

    • 组件里面加入了传递父组件的函数名称
    • 商品列表页里面也是需要接受的函数名称的
    • 因为主页里面加了函数但列表页没有,所以不起作用,解决办法就是在商品列表也也加入该函数
    1. <template>
    2. <view class='goods_list'> <!--加入灰色的背景-->
    3. <goods-list @goodsItemClick='goDetail' :goods="goods"></goods-list>
    4. <view class='isOver' v-if='flag'>---我是有底线的---</view>
    5. </view>
    6. </template>
    7. <script>
    8. import goodsList from '../../components/goods-list/goods_list.vue'
    9. export default {
    10. data() {
    11. return {
    12. pageindex:1,
    13. goods:[],
    14. flag:false
    15. };
    16. },
    17. components:{
    18. 'goods-list':goodsList
    19. },
    20. methods:{
    21. //获取商品列表数据
    22. async getGoodsList(callback){
    23. const res = await this.$myRequest({
    24. url : '/api/getgoods?pageindex='+this.pageindex
    25. })
    26. //this.goods = res.data.message
    27. this.goods = [...this.goods,...res.data.message]
    28. callback && callback() //如果有这个回调函数就调用
    29. },
    30. goDetail (id){
    31. uni.navigateTo({
    32. url:'/pages/goods-detail/goods-detail?id='+id
    33. })
    34. }
    35. },
    36. onLoad(){
    37. this.getGoodsList()
    38. },
    39. //下拉刷新
    40. onReachBottom() { //触底就会触发这个函数
    41. console.log('触底反弹')
    42. if(this.goods.length<10*(this.pageindex-1)){
    43. return this.flag = true
    44. }
    45. this.pageindex++ //页码书进行加1
    46. this.getGoodsList()
    47. },
    48. onPullDownRefresh() {
    49. console.log('下拉刷新了')
    50. this.pageindex = 1
    51. this.goods= []
    52. this.flag = false
    53. setTimeout(()=>{
    54. this.getGoodsList(()=>{
    55. uni.stopPullDownRefresh()
    56. })
    57. },1000)
    58. }
    59. }
    60. </script>
    61. <style lang="scss">
    62. .goods_list{
    63. background: #eee;
    64. }
    65. .isOver{
    66. width:100%;
    67. height:50px;
    68. line-height: 50px;
    69. text-align: center;
    70. font-size:28px;
    71. }
    72. </style>