vue3——递归组件的使用

  • 该文章是在学习 小满vue3 课程的随堂记录
  • 示例均采用 <script setup>,且包含 typescript 的基础用法

一、使用场景

递归组件 的使用场景,如 无限级的菜单 ,接下来就用菜单的例子来学习

二、具体使用

先把菜单的基础内容写出来再说

父组件

html 复制代码
<Tree :data="mock" />
ts 复制代码
// 引入子组件
import Tree from "../components/17_组件/Tree.vue";

// 菜单的数据格式
interface ITree {
  name: string;
  checked: boolean;
  children?: ITree[];
}
// 数据 mock
const mock: ITree[] = [
  {
    name: "1",
    checked: false,
    children: [
      {
        name: "1-1",
        checked: true,
      },
    ],
  },
  {
    name: "2",
    checked: false,
  },
  {
    name: "3",
    checked: false,
    children: [
      {
        name: "3-1",
        checked: false,
        children: [
          {
            name: "3-1-1",
            checked: true,
          },
        ],
      },
    ],
  },
];

子组件

html 复制代码
<!-- 第一层数据 -->
<div v-for="item in data" style="margin-left: 16px">
  <input type="checkbox" v-model="item.checked" />
  {{ item.name }}
</div>
ts 复制代码
<script setup lang="ts">
// 这里为了方便直接拷贝过来了,实际项目中要抽离出来复用
interface ITree {
  name: string;
  checked: boolean;
  children?: ITree[];
}
// props 定义
defineProps<{
  data?: ITree[];
}>();
</script>

如图,上述代码已经完成了 第一层 菜单数据的渲染:

深层 的菜单数据,就要用到 递归组件 了,递归组件有以下 三种 使用方式

1、直接使用 自己的文件名 作为 组件名称,不需要引入

当前的文件名称是 Tree.vue,所以直接使用 Tree 即可

子组件

html 复制代码
<!-- 第一层数据 -->
<div v-for="item in data" style="margin-left: 16px">
  <input type="checkbox" v-model="item.checked" />
  {{ item.name }}
  <!-- 需要注意:需要按照格式传入子组件要用的数据,并且加上递归的判断条件 -->
  <Tree v-if="item?.children?.length" :data="item.children"></Tree>
</div>

使用结果如下:

2、子组件中再定义一个 script,并暴露出 name

子组件

ts 复制代码
<script lang="ts">
// 再起一个 script(lang必须保持一致),用来定义该组件的 name
export default {
  name: "TreeVue",
};
</script>

使用时,就可以用自定义的组件名

html 复制代码
<!-- 第一层数据 -->
<div v-for="item in data" style="margin-left: 16px">
  <input type="checkbox" v-model="item.checked" />
  {{ item.name }}
  <!-- 深层数据 -->
  <TreeVue v-if="item?.children?.length" :data="item.children"></TreeVue>
</div>

渲染结果一致:

3、使用 defineOptions 自定义组件名

  • Vue3.3 增加了 defineOptions 方法,用来定义 optionsAPI 中的选项,比如组件名称 name(老一点的 vue 版本要装插件才能使用 defineOptions)
  • 与上面第二种思路一致,就是起个名再用

子组件

ts 复制代码
<script setup lang="ts">
interface ITree {
  name: string;
  checked: boolean;
  children?: ITree[];
}
defineProps<{
  data?: ITree[];
}>();

// 直接在原有的 script 中进行自定义
defineOptions({
  name: "Self",
});
</script>
html 复制代码
<!-- 第一层数据 -->
<div v-for="item in data" style="margin-left: 16px">
  <input type="checkbox" v-model="item.checked" />
  {{ item.name }}
  <!-- 深层数据 -->
  <Self v-if="item?.children?.length" :data="item.children"></Self>
</div>

渲染结果仍然一致,不再赘述

相关推荐
YUELEI11813 小时前
构建electron项目
electron·vue3
陈逸子风15 小时前
(系列五).net8 中使用Dapper搭建底层仓储连接数据库(附源码)
vue3·webapi·权限·流程
一雨方知深秋7 天前
Element-plus安装及其基础组件使用
javascript·css·html·vue3·element-plus
清晨-阳光zx7 天前
vue-i18n在使用$t时提示类型错误
vue3·vue-i18n
陈逸子风8 天前
从0到1搭建权限管理系统系列四 .net8 中Autofac的使用(附源码)
vue3·webapi·权限·流程·表单
苍风的心上人8 天前
uniapp 使用Vue3 setup引入 uniapp 的onReachBottom
uni-app·vue3
wocwin9 天前
Vue3 + element-plus el-table二次封装组件新增虚拟滚动功能
vue.js·vue3·el-table·element-plus·virtual·虚拟滚动·table虚拟列表
Mao.O10 天前
在线聊天室项目(Vue3 + SpringBoot)
spring boot·websocket·vue3·在线聊天室
流氓也是种气质 _Cookie11 天前
14 vue3之内置组件trastion全系列
vue3·gsap
这孩子叫逆12 天前
uni-app进行微信小程序开发,快速上手
微信小程序·vue3·uniapp·vant