获取api的数据,讲商品数据进行渲染!

    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'>
    11. <view class='iconfont icon-ziyuan'></view>
    12. <text>黑马超市</text>
    13. </view>
    14. <view class='nav_item'>
    15. <view class='iconfont icon-guanyuwomen'></view>
    16. <text>联系我们</text>
    17. </view>
    18. <view class='nav_item'>
    19. <view class='iconfont icon-tupian'></view>
    20. <text>社区图片</text>
    21. </view>
    22. <view class='nav_item'>
    23. <view class='iconfont icon-shipin'></view>
    24. <text>学习视频</text>
    25. </view>
    26. </view>
    27. <!--推荐商品-->
    28. <view class='hot_goods'>
    29. <view class='tit'>推荐商品</view>
    30. <view class='goods_list'>
    31. <view class='goods_item' v-for="item in goods" :key="item.id">
    32. <image :src='item.img_url'></image>
    33. <view class='price'>
    34. <text>¥{{item.sell_price}}</text>
    35. <text>¥{{item.market_price}}</text>
    36. </view>
    37. <view class="name">
    38. {{item.title}}
    39. </view>
    40. </view>
    41. </view>
    42. </view>
    43. </view>
    44. </template>
    45. <script>
    46. export default {
    47. data() {
    48. return {
    49. swipers:[],
    50. goods:[]
    51. }
    52. },
    53. onLoad() { //监听页面加载
    54. this.getSwiper()
    55. this.getHotGoods()
    56. },
    57. methods: {
    58. //获取轮播的数据
    59. // async getSwiper(){
    60. // console.log('获取轮播的数据')
    61. // const res = await uni.request({
    62. // url:'http://localhost:8082/api/getlunbo',
    63. // })
    64. // console.log(res.data)
    65. // }
    66. //获取轮播数据
    67. async getSwiper(){
    68. // console.log('获取轮播的数据')
    69. // uni.request({
    70. // url:'http://localhost:8082/api/getlunbo',
    71. // success:res=>{
    72. // console.log(res.data.message)
    73. // if(res.data.status !== 0){
    74. // return uni.showToast({
    75. // title:'获取数据失败'
    76. // })
    77. // }
    78. // this.swipers = res.data.message
    79. // }
    80. // })
    81. const res =await this.$myRequest({
    82. url:'/api/getlunbo'
    83. })
    84. this.swipers = res.data.message
    85. },
    86. //获取热门商品列表数据
    87. async getHotGoods(){
    88. const res = await this.$myRequest({
    89. url:'/api/getgoods?pageindex=1'
    90. })
    91. this.goods = res.data.message
    92. console.log(this.goods)
    93. }
    94. }
    95. }
    96. </script>
    97. <style lang="scss">
    98. .home{
    99. swiper{
    100. width:750rpx;
    101. height: 380rpx;
    102. image{
    103. height:100%;
    104. width:100%;
    105. }
    106. }
    107. .nav{
    108. display: flex;
    109. .nav_item{
    110. width:25%;
    111. text-align: center;
    112. view{
    113. width: 120rpx;
    114. height:120rpx;
    115. background-color: $shop-color;
    116. border-radius: 60rpx;
    117. margin: 10px auto;
    118. line-height: 120rpx;
    119. color:#fff;
    120. font-size:50rpx;
    121. }
    122. .icon-tupian{
    123. font-size:45rpx;
    124. }
    125. text{
    126. font-size: 30rpx;
    127. }
    128. }
    129. }
    130. .hot_goods{
    131. background: #eee;
    132. overflow:hidden;
    133. margin-top: 10px;
    134. .tit{
    135. height:50px;
    136. line-height: 50px;
    137. color:$shop-color;
    138. text-align: center;
    139. letter-spacing: 20px;
    140. font-size: 20px;
    141. margin:7rpx 0;
    142. background: #fff;
    143. }
    144. .goods_list{
    145. padding:0 15rpx;
    146. display: flex;
    147. flex-wrap: wrap;
    148. justify-content: space-between;
    149. .goods_item{
    150. background: #fff;
    151. width:355rpx;
    152. margin:10rpx 0;
    153. padding: 15rpx;
    154. box-sizing: border-box;//不会撑开盒子了
    155. image{
    156. width:95%;
    157. height:200px;
    158. display:block;
    159. margin:0 auto; //上面下面实现居中
    160. }
    161. .price{
    162. color:$shop-color;
    163. font-size: 36rpx;
    164. margin:5rpx 0;
    165. text:nth-child(2){
    166. color:#CCC;
    167. font-size: 28rpx;
    168. margin-left:17rpx;
    169. text-decoration: line-through;
    170. }
    171. }
    172. .name{
    173. font-size:22rpx;
    174. line-height:40rpx;
    175. }
    176. }
    177. }
    178. }
    179. }
    180. </style>

    image.png