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>

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

相关推荐
RuoyiOffice19 小时前
SpringBoot OA推荐:面向信创选型,看看这套企业协同平台
spring boot·vue3·信创·flowable·企业协同·ruoyi office·oa选型
周厉19 小时前
WebSocket 使用场景与实现方法:Java + Vue3 全栈实战
java·websocket·vue3·websocket双向通讯
红红谈说2 天前
同一套产品两个技术栈,双版本前端工程差异怎么收敛?
前端·uni-app·vue3·前后端分离·双版本维护·接口约定
RuoyiOffice3 天前
Flowable 状态通知设计:单体用本地事件,微服务如何回写业务状态?
spring boot·微服务·vue3·flowable·ruoyi office·redis stream·流程状态通知
AI开源软件3 天前
SpringBoot + Vue3 人力资源管理系统推荐:组织、档案与移动办公
vue3·springboot·ruoyi·oa系统·移动办公·人力资源管理系统·hrm
梅雅达编程笔记3 天前
Day 17 | uni-app 小程序:前端从零搭建
微信小程序·typescript·uni-app·vue3·pinia·前端开发·全栈
RuoyiOffice4 天前
项目预算和交付日期变了,为什么不能直接改台账?
spring boot·项目管理·vue3·flowable·业务建模·ruoyi office·项目变更
AI源码4 天前
图书馆借阅管理系统
vue3·springboot
RuoyiOffice5 天前
SpringBoot+Vue3 单据校验设计:保存草稿、提交审批、退回重填,各该检查什么?
spring boot·vue3·flowable·表单设计·ruoyi office·草稿保存·单据校验
RuoyiOffice7 天前
SpringBoot+Vue3 业财协同架构:预算、费用分摊、发票、往来与凭证怎样避免五套数字
spring boot·vue3·财务管理·spring boot 3·预算管理·ruoyi office·业财协同