api的应用

    1. <template>
    2. <view class='pics'>
    3. <scroll-view class='left' scroll-y>
    4. <view :class="active === index?'active':''" v-for='(item,index) in cates' :key='item.id' @click="leftClickHandle(index,item.id)">{{item.title}}</view>
    5. </scroll-view>
    6. <scroll-view class='right' scroll-y>
    7. <view class='item' v-for='item in secondData' :key='item.id'>
    8. <image @click='previewImg(item.img_url)' :src='item.img_url'></image>
    9. <text>{{item.title}}</text>
    10. </view>
    11. <text v-if='secondData.length === 0'>暂无数据</text>
    12. </scroll-view>
    13. </view>
    14. </template>
    15. <script>
    16. export default {
    17. data() {
    18. return {
    19. cates:[],
    20. active:0,
    21. secondData:[]
    22. };
    23. },
    24. methods:{
    25. async getPicsCate(){
    26. const res = await this.$myRequest({
    27. url:'/api/getimgcategory'
    28. })
    29. this.cates = res.data.message
    30. //console.log(this.cates)
    31. this.leftClickHandle(0,this.cates[0]['id'])
    32. },
    33. async leftClickHandle(index,id){ //点击一下就变成高亮
    34. this.active = index
    35. //console.log(id)
    36. //获取右侧的数据
    37. const res = await this.$myRequest({
    38. url:'/api/getimages/'+id
    39. })
    40. this.secondData = res.data.message
    41. },
    42. previewImg(current){
    43. const urls = this.secondData.map(item=>{
    44. return item.img_url
    45. })
    46. //console.log(urls)
    47. uni.previewImage({
    48. current,
    49. urls //键和数值一样可以缩写
    50. })
    51. }
    52. },
    53. onLoad(){
    54. this.getPicsCate()
    55. }
    56. }
    57. </script>
    58. <style lang="scss">
    59. page{
    60. height:100%;
    61. }
    62. .pics{
    63. height:100%;
    64. display: flex;
    65. .left{
    66. width:200rpx;
    67. height:100%;
    68. border-right:1px solid #eee;
    69. view{
    70. height:60px;
    71. line-height:60px;
    72. color:#333;
    73. text-align: center;
    74. font-size:35rpx;
    75. border-top:1px solid #eee;
    76. }
    77. .active{
    78. background: $shop-color;
    79. color:#fff;
    80. }
    81. }
    82. .right{
    83. height:100%;
    84. width:530rpx;
    85. margin:20rpx auto;
    86. .item{
    87. image{
    88. width:520rpx;
    89. height:520rpx;
    90. border-radius: 5px;
    91. }
    92. text{
    93. font-size: 30rpx;
    94. line-height: 50rpx;
    95. }
    96. }
    97. }
    98. }
    99. </style>

    image.png