Vue + Node v-for三层循环嵌套渲染数据

2021/11/21 14:09:58

本文主要是介绍Vue + Node v-for三层循环嵌套渲染数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

后端(node简易版)接口数据:

//分类的接口
router.get('/api/goods/list', function (req, res, next) {
  res.send({
    code: 0,
    data:
      [
        {
          //一级
          id: 0,
          name: '推荐',
          data: [
            {
              //二级
              id: 0,
              name: '推荐',
              list: [
                //三级
                {
                  id: 0,
                  name: '铁观音',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 1,
                  name: '功夫茶具',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 2,
                  name: '茶具电器',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 3,
                  name: '紫砂壶',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 4,
                  name: '龙井',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 5,
                  name: '武夷岩茶',
                  imgUrl: './images/list1.jpeg'
                },
              ]
            }

          ]
        },
        {
          //一级
          id: 1,
          name: '绿茶',
          data: [
            {
              //二级
              id: 1,
              name: '绿茶',
              list: [
                //三级
                {
                  id: 0,
                  name: '龙井绿茶',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 1,
                  name: '碧螺春',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 2,
                  name: '雀舌',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 3,
                  name: '安吉白茶',
                  imgUrl: './images/list1.jpeg'
                },
                {
                  id: 4,
                  name: '六安瓜片',
                  imgUrl: './images/list1.jpeg'
                }
              ]
            }
          ]
        },
      ]
  })
})

List.vue>

 <section ref="wrapper">
      <!-- 左边 -->
      <div class="list-l">
        <ul class="l-item">
          <li class="active" v-for="(item, index) in leftData" :key="index">
            {{ item.name }}
          </li>
        </ul>
      </div>
      <!-- 右边 -->
      <div class="list-r">
        <!-- 数据结构是三层数组嵌套 所以循环三次 -->
        <ul v-for="(item, index) in rightData" :key="index">
          <li v-for="(v, i) in item" :key="i">
            <h2>{{ v.name }}</h2>
            <ul class="r-content">
              <li class="shop-list" v-for="(k, idx) in v.list" :key="idx">
                <img :src="k.imgUrl" alt="" />
                <span>{{ k.name }}</span>
              </li>
            </ul>
          </li>
        </ul>
      </div>
</section>

import http from "@/common/api/request.js";

 data() {
    return {
      leftData: [], //左侧数据
      rightData: [], //右侧数据
    };
  },
  async created() {
    let res = await http.$axios({
      url: "/api/goods/list",
    });
    let leftArr = [];
    let rightArr = [];
    //重构建数组再赋值 循环res:[{…}, {…}, {…}, {…}, {…}, {…}], 使leftArr每一项为一个对象
    res.forEach((v) => {
      leftArr.push({
        id: v.id,
        name: v.name,
      });
      rightArr.push(v.data);
    });
    console.log(leftArr); //[{id: 0, name: '推荐'}, {…}, {…}, {…}, {…}, {…}]
    console.log(rightArr);
    this.leftData = leftArr;
    this.rightData = rightArr;
  },



这篇关于Vue + Node v-for三层循环嵌套渲染数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程