递归组件具名插槽失效的问题

文章讲述了在Vue中使用listview组件时遇到的递归问题,通过修改模板和使用`v-for`以及`v-slot`解决了子组件数据传递和插槽失效的问题,演示了如何在父组件中管理子组件的层级结构。
摘要由CSDN通过智能技术生成

//listview组件 递归的组件

<template>

  <div>

    <div class="list-item" v-for="(item, index) in list" :key="index">

      <div class="item-name" :style="item.style">

        <slot :name="item.value" :scope="item">{{ item.name }}</slot>

      </div>

      <div v-if="item.children" class="children-item">

        <listview :list="item.children">

//这里是主要解决递归组件失效的问题

          <template

            v-for="(item1, index1) in Object.keys($slots)"

            :key="index1"

            v-slot:[item1]

          >

//scope是插槽传递的数据

            <slot :name="item1" :scope="getScopeData(item1, list)" />

          </template>

        </listview>

      </div>

    </div>

  </div>

</template>

<script setup name="listview">

import listview from "./index.vue";

const props = defineProps({

  list: {

    type: Array,

    default: () => [],

  },

});

const getScopeData = (name, data) => {

  const findItem = (name, data) => {

    for (const item of data) {

      if (item.value === name) {

        return item;

      }

      if (item.children && item.children.length) {

        const result = findItem(name, item.children);

        if (result) {

          return result;

        }

      }

    }

    return null;

  };

  const scope = findItem(name, data);

  return scope;

};

</script>

//父组件

<listview :list="list">

        <template #jingji="{ scope }">

          <div>{{ `${scope.name}插槽` }}</div>

        </template>

        <template #rujia="{ scope }">

          123==={{ scope.name }}

        </template>

        <template #qitian="{ scope }">

          123==={{ scope.name }}

        </template>

        <template #rujia-1="{ scope }">

          123==={{ scope.name }}

        </template>

      </listview>

import listview from "@/components/listview";

const list = [

  {

    name: "经济",

    value: "jingji",

    id: "1",

    children: [

      {

        name: "如家",

        value: "rujia",

        style: "margin-left: 10px;",

        id: "1-1",

        children: [

          {

            style: "margin-left: 20px;",

            name: "上江路-如家",

            value: "rujia-1",

            id: "1-1-1",

            children: [

              {

                style: "margin-left: 30px;",

                name: "上江路-如家-3",

                id: "1-1-1-1",

                value: "rujia-2",

              },

            ],

          },

          {

            style: "margin-left: 20px;",

            name: "望江路-如家",

            id: "1-1-2",

          },

        ],

      },

      {

        name: "7天",

        id: "1-2",

        value: "qitian",

        style: "margin-left: 10px;",

        children: [

          {

            name: "长江路-7天",

            style: "margin-left: 20px;",

            id: "1-2-1",

          },

          {

            name: "望江路-7天",

            style: "margin-left: 20px;",

            id: "1-2-2",

          },

        ],

      },

    ],

  },

];

  • 14
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值