vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue组件递归及应用

vue业务实例之组件递归及其应用

作者:surewinT

目中出现多级菜单时,需要多层for循环时,但是当菜单增加层级时,需要在页面结构中增加一层for循环,这时我们可以使用组件递归的思想来实现,下面这篇文章主要给大家介绍了关于vue业务实例之组件递归及其应用的相关资料,需要的朋友可以参考下

递归简介

含义:程序调用自身的编程技巧称为递归,那组件调用自身就是组件递归

应用场景:在实际业务开发中,通常应用于菜单栏、树组件、多级下拉框等

vue实现组件递归

调用效果及代码

<!--
 * @Date: 2020-12-09 17:52:54
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-10 14:14:15
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 调用页面
-->

<template>
    <div class="">
        <div v-for="(item, index) in testList" :key="index">
            <Test :listitem="item" />
        </div>
    </div>
</template>

<script>
import Test from "./test.vue";
export default {
    components: {
        Test,
    },
    props: [],
    data() {
        return {
            testList: [
                {
                    name: "你好啊",
                    value: "1",
                    children: [
                        {
                            name: "你好啊",
                            value: "1-1",
                        },
                        {
                            name: "你好啊",
                            value: "1-2",
                            children: [
                                {
                                    name: "你好啊",
                                    value: "1-2-1",
                                },
                            ],
                        },
                    ],
                },
                {
                    name: "好的呢",
                    value: "2",
                    children: [],
                },
            ],
        };
    },
    mounted() {},
    watch: {},
    methods: {},
};
</script>

<style lang='scss' scoped>
.p-el-menu {
    width: 200px;
}
</style>

组件递归案例

需要递归的组件,必须写组件名name,可以在代码中直接使用name进行调用

<!--
 * @Date: 2022-05-10 11:30:50
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-10 14:15:09
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 组件递归案例
-->

<template>
    <div class="test-root">
        {{ `${listitem.value}:${listitem.name}` }}
        <div v-for="(item, index) in listitem.children" :key="index">
            <!-- 直接使用自身组件 -->
            <Test :listitem="item" />
        </div>
    </div>
</template>

<script>
export default {
    // 必须写name
    name: "Test",
    components: {},
    props: ["listitem"],
    data() {
        return {};
    },
    mounted() {},
    watch: {},
    methods: {},
};
</script>

<style lang='scss' scoped>
.test-root {
    padding: 20px;
    display: inline-block;
    border: 1px solid #409eff;
    margin: 10px 0;
}
</style>

递归实现菜单栏

调用效果及代码

<!--
 * @Date: 2020-12-09 17:52:54
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-10 14:20:32
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 调用页面
-->

<template>
    <div class="">
        <el-menu class="p-el-menu">
            <Menutree :menuList="menuList" />
        </el-menu>
    </div>
</template>

<script>
import Menutree from "./p-el-menu.vue";
export default {
    components: {
        Menutree,
    },
    props: [],
    data() {
        return {
            menuList: [
                {
                    label: "菜单1",
                    value: "1",
                    children: [
                        {
                            label: "菜单1-1",
                            value: "1-1",
                        },
                        {
                            label: "菜单1-2",
                            value: "1-2",
                            children: [
                                {
                                    label: "菜单1-2-1",
                                    value: "1-2-1",
                                },
                            ],
                        },
                    ],
                },
                {
                    label: "菜单2",
                    value: "2",
                    childern: [],
                },
                {
                    label: "菜单3",
                    value: "3",
                    children: [
                        {
                            label: "菜单3-1",
                            value: "3-1",
                        },
                    ],
                },
            ],
        };
    },
    mounted() {},
    watch: {},
    methods: {},
};
</script>

<style lang='scss' scoped>
.p-el-menu {
    width: 200px;
}
</style>

递归生成菜单

<!--
 * @Date: 2022-05-10 11:45:08
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-10 14:28:58
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 递归生成菜单
-->

<template>
    <div class="">
        <div v-for="(menu, index) in menuList" :key="index">
            <el-menu-item v-if="!menu.children || menu.children.length == 0" :index="menu.value">
                <i :class="menu.icon ? menu.icon : 'el-icon-menu'"></i>
                <span slot="title" class="group_title">{{ menu.label }}</span>
            </el-menu-item>
            <el-submenu v-else :index="menu.value">
                <template slot="title">
                    <i :class="menu.icon ? menu.icon : 'el-icon-menu'"></i>
                    <span>{{ menu.label }}</span>
                </template>
                <!-- 递归自身 -->
                <p-el-menu :menuList="menu.children" />
            </el-submenu>
        </div>
    </div>
</template>

<script>
export default {
    // 必须写name
    name: "p-el-menu",
    components: {},
    props: ["menuList"],
    data() {
        return {};
    },
    mounted() {},
    watch: {},
    methods: {},
};
</script>

<style lang='scss' scoped>
</style>

仓库源码

vue组件递归

总结

到此这篇关于vue业务实例之组件递归及其应用的文章就介绍到这了,更多相关vue组件递归及应用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文