布局分析

  • 水平方向
    • 页面左侧定宽 右侧 自然撑开
  • 垂直方向
    • 头尾 定高 中间自然撑开
      • height:calc(100vh - XXrpx)内容减去顶部导航的高度
  • 左侧滚动
    • overflow: auto显示滚动条
  • 右侧滚动 ```vue

  1. <a name="b7W3d"></a>
  2. ### 数据分析及处理
  3. > 数据是一次性返回全部的条例,将数组拆分成两部分
  4. > 左侧的标题数据 右侧的商品数组
  5. 根据当前项的下标动态添加选中类<br />样式使用伪元素
  6. ```vue
  7. <template>
  8. <view>
  9. <Search></Search>
  10. <view class="content">
  11. <view class="menu">
  12. <view :class="['menu-item', current === index? 'active' : '']" v-for="(item,index) in menus" :key="item">
  13. {{item}}
  14. </view>
  15. </view>
  16. <view class="goods">
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. // 当前选中项的下标
  26. current: 0,
  27. // 菜单数据
  28. menus:[],
  29. }
  30. },
  31. methods: {
  32. async getCategories() {
  33. const res = await this.$u.get('/categories');
  34. console.log(res);
  35. this.menus = res.message.map(item => item.cat_name)
  36. },
  37. handleActive(index) {
  38. this.current = index
  39. }
  40. },
  41. onLoad() {
  42. this.getCategories()
  43. }
  44. }
  45. </script>
  46. <style lang="scss">
  47. .content{
  48. height: calc(100vh - 64rpx);
  49. display: flex;
  50. .menu{
  51. width: 182rpx;
  52. overflow: auto;
  53. .menu-item{
  54. font-size: 28rpx;
  55. height: 72rpx;
  56. display: flex;
  57. justify-content: center;
  58. align-items: center;
  59. }
  60. .active{
  61. position: relative;
  62. color: #EA4350;
  63. &::before{
  64. content: "";
  65. position: absolute;
  66. width: 3px;
  67. height: 80%;
  68. top: 50%;
  69. transform: translateY(-50%);
  70. left: 0;
  71. background-color: #eb4450;
  72. }
  73. }
  74. }
  75. .goods{
  76. flex: 1;
  77. overflow: auto;
  78. }
  79. }
  80. </style>

将商品数据取出来,根据数据结构两层循环渲染

  • uniapp中规定
    • 如果在 data中定义过变量
      • 全局变量 可以在任意地方 this.xx 方式来使用
      • 这个变量编译到小程序的时候,会被存放在小程序data - 在标签中直接使用
    • 如果没有在data中定义 变量 而是在 其他地方直接 写 this.xxx
      • this.xxx 也是全局变量
      • 不会把这个数据 编译到小程序data中, 在标签中 无法使用它!!

        如果只在uniapp中使用 页面级别的全局变量 直接this.xxx 即可使用 不需要在data中定义 在data中定义的变量一定要在标签中直接使用 在data中定义的数据都要在标签中体现出来 否则 就是不好的代码 影响性能小程序体验会特卡!!!

  1. <template>
  2. <view>
  3. <view class="content">
  4. <view class="goods">
  5. <view
  6. class="goods-item"
  7. v-for="item in goods"
  8. :key="item.cat_id">
  9. <!-- 标题 -->
  10. <view class="goods-title">
  11. {{ item.cat_name }}
  12. </view>
  13. <!-- 商品 -->
  14. <view class="goods-modle">
  15. <navigator url=""
  16. v-for="good in item.children"
  17. :key="good.cat_id">
  18. <!-- 图片 -->
  19. <u-image
  20. mode="widthFix"
  21. width="100%"
  22. :src="good.cat_icon">
  23. </u-image>
  24. <!-- 文字 -->
  25. <text>{{ good.cat_name }}</text>
  26. </navigator>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data () {
  36. return {
  37. // 当前选中项的下标
  38. current: 0,
  39. // 商品数据
  40. goods: []
  41. }
  42. },
  43. methods: {
  44. async getCategories () {
  45. const res = await this.$u.get('/categories')
  46. console.log(res)
  47. /**
  48. * 如果变量没由使用在页面中 可以不在data中定义 直接使用
  49. * 这样性能会更好一点
  50. */
  51. this.categories = res.message
  52. // 下标0 这里为默认
  53. this.goods = this.categories[0].children
  54. },
  55. handleActive (index) {
  56. this.current = index
  57. // 根据下标切换不同的商品内容
  58. this.goods = this.categories[this.current].children
  59. }
  60. },
  61. onLoad () {
  62. this.getCategories()
  63. }
  64. }
  65. </script>
  66. <style lang="scss">
  67. .goods {
  68. flex: 1;
  69. overflow: auto;
  70. .goods-item {
  71. .goods-title {
  72. font-size: 26rpx;
  73. height: 72rpx;
  74. display: flex;
  75. justify-content: center;
  76. align-items: center;
  77. &::before {
  78. content: '/';
  79. color: #ccc;
  80. margin-right: 20rpx;
  81. }
  82. &::after {
  83. content: '/';
  84. color: #ccc;
  85. margin-left: 20rpx;
  86. }
  87. }
  88. .goods-modle {
  89. display: flex;
  90. flex-wrap: wrap;
  91. navigator {
  92. width: 33.33%;
  93. padding: 15rpx;
  94. text-align: center;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. </style>