vue组件递归使用

博客介绍了Vue组件递归使用的相关内容,包含效果展示和代码实现。代码部分分为父组件和递归子组件list - table.vue,通过实例展示了Vue组件递归使用的方法。

vue组件递归使用,废话少说直接看实列

1、效果

 

2、代码

2.1、父组件

<template>
    <div>

        <list-table
            @editFunction="editFunction"
            @addSubFunction="addSubFunction"
            :list="list">
        </list-table>

        <br><br><br>
    </div>
</template>

<script>
    import listTable from "./list-table.vue";
    export default {
        components: {
            listTable
        },
        data () {
            return {
                //表格数据
                list: [],
            }
        },
        computed: {
            dict () {
                return this.$store.state.dict.urlResourcesMange;
            }
        },
        methods: {
            //查询列表
            search (page) {
                var _this = this;

               var res = {
                   "total": 0,
                   "data": [{
                       "createdBy": "0",
                       "creationDate": "2017-03-24 18:36:06",
                       "lastUpdatedBy": "0",
                       "lastUpdateDate": "2018-04-09 19:42:45",
                       "resourceId": "7",
                       "systemCode": "tms",
                       "code": "jcsz",
                       "name": "基础设置",
                       "url": "",
                       "type": "menu",
                       "orderSort": 0,
                       "all": 0,
                       "menuRouterUrl": "",
                       "menuIcon": "",
                       "status": "enable",
                       "children": [{
                           "createdBy": "0",
                           "creationDate": "2017-03-24 18:37:41",
                           "lastUpdatedBy": "0",
                           "lastUpdateDate": "2019-03-22 16:12:27",
                           "resourceId": "8",
                           "systemCode": "tms",
                           "code": "jcsz-spgl",
                           "name": "商品管理",
                           "url": "/api/goods/list",
                           "type": "menu",
                           "orderSort": 2,
                           "all": 1,
                           "menuRouterUrl": "/goods/list",
                           "menuIcon": "",
                           "status": "enable",
                           "children": [{
                               "createdBy": "0",
                               "creationDate": "2017-03-24 18:39:02",
                               "lastUpdatedBy": "0",
                               "lastUpdateDate": "2018-08-08 17:27:01",
                               "resourceId": "9",
                               "systemCode": "tms",
                               "code": "jcsz-bj",
                               "name": "编辑",
                               "description": "编辑(商品管理)",
                               "url": "/api/goods/update/{goodsId}",
                               "type": "api",
                               "orderSort": 0,
                               "all": 0,
                               "menuRouterUrl": "",
                               "menuIcon": "",
                               "status": "enable",
                               "children": [{
                                   "createdBy": "0",
                                   "creationDate": "2017-03-24 18:39:02",
                                   "lastUpdatedBy": "0",
                                   "lastUpdateDate": "2018-08-08 17:27:01",
                                   "resourceId": "9",
                                   "systemCode": "tms",
                                   "code": "jcsz-bj",
                                   "name": "编辑",
                                   "description": "编辑(商品管理)",
                                   "url": "/api/goods/update/{goodsId}",
                                   "type": "api",
                                   "orderSort": 0,
                                   "all": 0,
                                   "menuRouterUrl": "",
                                   "menuIcon": "",
                                   "status": "enable",
                                   children: []
                               }],
                               "pid": "8"
                           }, {
                               "createdBy": "0",
                               "creationDate": "2017-03-24 18:39:57",
                               "lastUpdatedBy": "0",
                               "lastUpdateDate": "2018-07-04 09:56:04",
                               "resourceId": "10",
                               "systemCode": "tms",
                               "code": "jcsz-dr",
                               "name": "导入",
                               "description": "导入(商品管理)",
                               "url": "/api/goods/import",
                               "type": "api",
                               "orderSort": 0,
                               "all": 0,
                               "menuRouterUrl": "",
                               "menuIcon": "",
                               "status": "disable",
                               "children": [],
                               "pid": "8"
                           }, {
                               "createdBy": "0",
                               "creationDate": "2017-03-24 18:40:25",
                               "lastUpdatedBy": "0",
                               "lastUpdateDate": "2018-04-09 19:42:46",
                               "resourceId": "11",
                               "systemCode": "tms",
                               "code": "jcsz-dc",
                               "name": "导出",
                               "description": "导出(商品管理)",
                               "url": "/api/goods/export",
                               "type": "api",
                               "orderSort": 0,
                               "all": 0,
                               "menuRouterUrl": "",
                               "menuIcon": "",
                               "status": "enable",
                               "children": [],
                               "pid": "8"
                           }, {
                               "createdBy": "0",
                               "creationDate": "2018-03-21 17:59:05",
                               "lastUpdatedBy": "0",
                               "lastUpdateDate": "2018-04-09 19:43:24",
                               "resourceId": "1065",
                               "systemCode": "tms",
                               "code": "jcsz-spgl-szclx",
                               "name": "设置拆零箱",
                               "description": "设置拆零箱(基础设置-商品管理)",
                               "url": "/api/tms/box/insertOrUpdate",
                               "type": "api",
                               "orderSort": 0,
                               "all": 0,
                               "menuRouterUrl": "",
                               "menuIcon": "",
                               "status": "enable",
                               "children": [],
                               "pid": "8"
                           }, {
                               "createdBy": "0",
                               "creationDate": "2018-06-25 13:46:38",
                               "lastUpdatedBy": "0",
                               "lastUpdateDate": "2018-06-25 13:46:38",
                               "resourceId": "1662",
                               "systemCode": "tms",
                               "code": "jcsz-spgl-cjsq",
                               "name": "创建申请",
                               "description": "创建申请(基础设置-商品管理)",
                               "url": "/api/goods/importMaintenace",
                               "type": "api",
                               "orderSort": 0,
                               "all": 0,
                               "menuRouterUrl": "",
                               "menuIcon": "",
                               "status": "enable",
                               "children": [],
                               "pid": "8"
                           }],
                           "pid": "7"
                       }]
                   }],
                   "desc": "成功",
                   "code": "200"
               };
                _this.list = res.data;
            },
            //获取子组件事件
            editFunction (data,type) {
                console.log(data,type)
            },
            //获取子组件事件
            addSubFunction (data,type) {
                console.log(data,type)
            },
        },
        created: function () {
            var _this = this;

            _this.search();
        }
    }
</script>

2.2 递归子组件:list-table.vue

<template>
    <div class="list-item">
        <el-table
            class="mt10px"
            :data="list"
            border
            :row-class-name="tableRowClassName"
            style="width: 100%">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <list-table
                        @editFunction="editFunction"
                        @addSubFunction="addSubFunction"
                        v-if="props.row.children.length > 0"
                        :list="props.row.children">
                    </list-table>
                </template>
            </el-table-column>

            <el-table-column
                label="序号"
                width="65"
                type="index"
                align="center">
            </el-table-column>

            <el-table-column
                label="名称"
                prop="name">
            </el-table-column>

            <el-table-column
                label="编码"
                prop="code"
                min-width="150px">
            </el-table-column>

            <el-table-column
                label="描述"
                prop="description">
            </el-table-column>

            <el-table-column
                label="类型">
                <template scope="scope">
                    {{dict.type[scope.row.type]}}
                </template>
            </el-table-column>

            <el-table-column
                label="排序"
                prop="orderSort">
            </el-table-column>

            <el-table-column
                label="状态">
                <template scope="scope">
                    {{dict.status[scope.row.status]}}
                </template>
            </el-table-column>

            <el-table-column
                label="请求api"
                prop="url"
                min-width="200px">
            </el-table-column>

            <el-table-column
                label="菜单路由"
                prop="menuRouterUrl"
                min-width="200px">
            </el-table-column>

            <el-table-column
                label="操作"
                width="220"
                align="center">
                <template scope="scope">
                    <el-button  type="info" @click="editFunction(scope.row,'edit')">编辑</el-button>
                    <el-button  type="warning" @click="changeStatus(scope.row.resourceId,scope.row)">
                        {{dict.status[scope.row.status]}}
                    </el-button>
                    <el-button  type="success" @click="addSubFunction(scope.row,'addChild')">添加子资源</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
    export default {
        name: "listTable",
        props: {
            list: Array
        },
        computed: {
            dict () {
                return this.$store.state.dict.urlResourcesMange;
            }
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (row.children.length > 0) {
                    return 'url-resources-expand';
                } else {
                    return 'url-resources-hide';
                }

                return '';
            },

            editFunction (row,type) {
                this.$emit('editFunction', row,type);
            },

            addSubFunction (row,type) {
                this.$emit('addSubFunction', row,type);
            },

            changeStatus(resourceId, row) {

                var _this = this,
                    status = row.status == 'enable' ? 'disable' : 'enable',
                    statusStr = {
                        enable: '停用',
                        disable: '启用'
                    };

                _this.$confirm('确认' + statusStr[row.status] + '? ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(function () {
                    _this.$get({
                        url: '/api/manager/sysResource/status/' + resourceId + '/' + status,
                        success: function (res) {
                            _this.$message.success(statusStr[row.status] + '成功!');
                            row.status = status
                        }
                    });
                }, function () {});
            }
        }
    };
</script>
<style>
    .url-resources-hide .el-table__expand-icon {
        display: none !important;
    }
</style>

 

### Vue递归组件使用方法 在 Vue 中,递归组件是一种特殊的组件形式,它可以在其模板中调用自己来实现嵌套结构。为了使组件能够递归调用自身,必须为其定义 `name` 属性[^1]。 #### 基本原理 递归组件的核心在于通过 `v-if` 或其他逻辑控制语句,在满足特定条件下停止递归渲染,从而避免无限循环。通常会传递一个参数(如层级计数器),并通过判断该参数决定是否继续渲染子组件--- #### 示例代码:基于层级限制的递归组件 以下是一个简单的例子,展示如何通过 `count` 参数限制递归层数: ```vue <template> <div> <!-- 当前层的内容 --> <p>当前层级: {{ count }}</p> <!-- 判断是否继续递归 --> <my-component :count="count + 1" v-if="count <= 5" ></my-component> </div> </template> <script> export default { name: 'MyComponent', // 定义组件名称以便递归调用 props: { count: { // 接收父级传递的层级参数 type: Number, default: 1 } } } </script> ``` 在这个示例中,`v-if="count <= 5"` 控制了递归的最大深度为 5 层[^1]。 --- #### 实际应用:树形菜单的递归组件 对于更复杂的场景,例如构建动态加载的数据结构(如树形菜单),可以利用递归组件处理多层级数据。以下是具体实现方式: ##### 组件代码 (`ReSubmenu.vue`) ```vue <template> <a-submenu> <!-- 菜单标题 --> <div slot="title">{{ parent.title }}</div> <!-- 遍历子节点 --> <template v-for="(item, i) in parent.children"> <!-- 如果没有子节点,则显示为普通菜单项 --> <a-menu-item v-if="!item.children" :key="`menu_item_${index}_${i}`" > {{ item.title }} </a-menu-item> <!-- 否则递归调用自身 --> <re-submenu v-else :key="`submenu_${index}_${i}`" :parent="item" /> </template> </a-submenu> </template> <script> import menuComponents from '_c/menu'; const { AMenuItem, ASubmenu } = menuComponents; export default { name: 'ReSubmenu', // 必须显式声明组件名 components: { AMenuItem, ASubmenu }, props: { parent: { type: Object, default: () => ({}) }, index: { type: String, default: '' } } }; </script> ``` 此示例展示了如何根据数据中的 `children` 字段递归生成子菜单[^3]。 --- #### 关键点总结 1. **命名必要性** 递归组件必须具有 `name` 属性,这是 Vue 解析组件的关键部分。 2. **终止条件** 使用 `v-if` 或类似的逻辑表达式防止无限递归,确保组件能够在适当时候停止渲染。 3. **性能优化** 对于大数据量或深层嵌套的情况,建议结合懒加载或其他技术减少初始渲染开销。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值