image.png
    新建组件目录。goods_list的代码如下:

    1. <template>
    2. <view class='goods_list'>
    3. <view class='goods_item' v-for="item in goods" :key="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. }
    19. </script>
    20. <style lang='scss'>
    21. .goods_list {
    22. padding: 0 15rpx;
    23. display: flex;
    24. flex-wrap: wrap;
    25. justify-content: space-between;
    26. .goods_item {
    27. background: #fff;
    28. width: 355rpx;
    29. margin: 10rpx 0;
    30. padding: 15rpx;
    31. box-sizing: border-box; //不会撑开盒子了
    32. image {
    33. width: 95%;
    34. height: 200px;
    35. display: block;
    36. margin: 0 auto; //上面下面实现居中
    37. }
    38. .price {
    39. color: $shop-color;
    40. font-size: 36rpx;
    41. margin: 5rpx 0;
    42. text:nth-child(2) {
    43. color: #CCC;
    44. font-size: 28rpx;
    45. margin-left: 17rpx;
    46. text-decoration: line-through;
    47. }
    48. }
    49. .name {
    50. font-size: 22rpx;
    51. line-height: 40rpx;
    52. }
    53. }
    54. }
    55. </style>

    原来主页的代码index.vue 如下:

    1. <template>
    2. <view class='home'>
    3. <swiper indicator-dots circular>
    4. <swiper-item v-for='item in swipers' :key='item.id'>
    5. <image :src='item.img'></image>
    6. </swiper-item>
    7. </swiper>
    8. <!--导航-->
    9. <view class='nav'>
    10. <view class='nav_item' v-for="(item,index) in navs" :key='index' @click="navItemClick(item.path)">
    11. <view :class='item.icon'></view>
    12. <text>{{item.title}}</text>
    13. </view>
    14. </view>
    15. <!--推荐商品-->
    16. <view class='hot_goods'>
    17. <view class='tit'>推荐商品</view>
    18. <goods-list :goods='goods'></goods-list>
    19. </view>
    20. </view>
    21. </template>
    22. <script>
    23. import goodsList from '../../components/goods-list/goods_list.vue'
    24. export default {
    25. data() {
    26. return {
    27. swipers: [],
    28. goods: [],
    29. navs:[
    30. {
    31. icon:'iconfont icon-ziyuan',
    32. title:'黑马超市',
    33. path:'/pages/goods/goods'
    34. },
    35. {
    36. icon:'iconfont icon-guanyuwomen',
    37. title:'联系我们',
    38. path:'/pages/contact/contact'
    39. },
    40. {
    41. icon:'iconfont icon-tupian',
    42. title:'社区图片',
    43. path:'/pages/pics/pics'
    44. },
    45. {
    46. icon:'iconfont icon-shipin',
    47. title:'学习视频',
    48. path:'/pages//videos/videos'
    49. }
    50. ]
    51. }
    52. },
    53. onLoad() { //监听页面加载
    54. this.getSwiper()
    55. this.getHotGoods()
    56. },
    57. components:{"goods-list":goodsList},
    58. methods: {
    59. //获取轮播数据
    60. async getSwiper() {
    61. const res = await this.$myRequest({
    62. url: '/api/getlunbo'
    63. })
    64. this.swipers = res.data.message
    65. },
    66. //获取热门商品列表数据
    67. async getHotGoods() {
    68. const res = await this.$myRequest({
    69. url: '/api/getgoods?pageindex=1'
    70. })
    71. this.goods = res.data.message
    72. },
    73. //导航点击的处理
    74. navItemClick(url){
    75. //console.log('跳转')
    76. uni.navigateTo({
    77. url
    78. })
    79. }
    80. }
    81. }
    82. </script>
    83. <style lang="scss">
    84. .home {
    85. swiper {
    86. width: 750rpx;
    87. height: 380rpx;
    88. image {
    89. height: 100%;
    90. width: 100%;
    91. }
    92. }
    93. .nav {
    94. display: flex;
    95. .nav_item {
    96. width: 25%;
    97. text-align: center;
    98. view {
    99. width: 120rpx;
    100. height: 120rpx;
    101. background-color: $shop-color;
    102. border-radius: 60rpx;
    103. margin: 10px auto;
    104. line-height: 120rpx;
    105. color: #fff;
    106. font-size: 50rpx;
    107. }
    108. .icon-tupian {
    109. font-size: 45rpx;
    110. }
    111. text {
    112. font-size: 30rpx;
    113. }
    114. }
    115. }
    116. .hot_goods {
    117. background: #eee;
    118. overflow: hidden;
    119. margin-top: 10px;
    120. .tit {
    121. height: 50px;
    122. line-height: 50px;
    123. color: $shop-color;
    124. text-align: center;
    125. letter-spacing: 20px;
    126. font-size: 20px;
    127. margin: 7rpx 0;
    128. background: #fff;
    129. }
    130. }
    131. }
    132. </style>

    商品列表页的代码如下:

    1. <template>
    2. <view class='goods_list'> <!--加入灰色的背景-->
    3. <goods-list :goods="goods"></goods-list>
    4. </view>
    5. </template>
    6. <script>
    7. import goodsList from '../../components/goods-list/goods_list.vue'
    8. export default {
    9. data() {
    10. return {
    11. pageindex:1,
    12. goods:[]
    13. };
    14. },
    15. components:{
    16. 'goods-list':goodsList
    17. },
    18. methods:{
    19. //获取商品列表数据
    20. async getGoodsList(){
    21. const res = await this.$myRequest({
    22. url : '/api/getgoods?pageindex='+this.pageindex
    23. })
    24. this.goods = res.data.message
    25. }
    26. },
    27. onLoad(){
    28. this.getGoodsList()
    29. }
    30. }
    31. </script>
    32. <style lang="scss">
    33. .goods_list{
    34. background: #eee;
    35. }
    36. </style>

    注意上面获取商品的数据,还是调用了封装的方法!
    image.png