vue实现树形标题(组件自递归)

本文介绍了如何使用Vue实现树形标题的功能,重点在于组件的递归调用和通过props传递参数来定制标题样式。

被问道了,当时只是简单设计了一下,type等都没加上,重新实现了一下。

效果

在这里插入图片描述

// 数据结构
nodes: [
                    {
                        label: 'title 1',
                        content: 'hello world',
                        children: [
                            {
                                label: 'title 1-1',
                                content: 'hello world content',
                                children: [
                                    {
                                        label: 'title 1-1-1',
                                        content: 'hello world content',
                                        children: []
                                    },
                                    {
                                        label: 'title 1-1-2',
                                        content: 'hello world content',
                                        children: []
                                    }
                                ]
                            },
                            {
                                label: 'title 1-2',
                                content: 'hello world content',
                                children: [
                                    {
                                        label: 'title 1-2-1',
                                        content: 'hello world content',
                                        children: []
                                    },
                                ]
                            },
                            {
                                label: 'title 1-3',
                                content: 'hello world content',
                                children: []
                            }
                        ]
                    },
                    {
                        label: 'title 2',
                        content: 'hello world',
                        children: [
                            {
                                label: 'title 2-1',
                                content: 'hello world content',
                                children: []
                            },
                            {
                                label: 'title 2-2',
                                content: 'hello world content',
                                children: [
                                    {
                                        label: 'title 2-2-1',
                                        content: 'hello world content',
                                        children: []
                                    },
                                    {
                                        label: 'title 2-2-2',
                                        content: 'hello world content',
                                        children: [
                                            {
                                                label: 'title 2-2-2-1',
                                                content: 'hello world content',
                                                children:[]
                                            }
                                        ]
                                    }
                                ]
                            },

                        ]
                    }, {
                        label: 'title 3',
                        content: 'hello world',
                        children: []
                    }
                ]

实现

重点就是组件递归。简单的props传参。在根据传参调整标题样式。
组件设计如下:

    <script type="text/x-template" id='title'>
        <div v-if="nodes.length > 0">
            <div v-for="node in nodes" :style="strong">
                {{node.label}}
                <p :style="content">{{node.content}}</p>
                <comp-title :nodes="node.children" :type="type + 1"></comp-title>
            </div>
        </div>
    </script>

	Vue.component('comp-title', {
            template: '#title',
            props: {
                nodes: {
                    type: Array,
                    default: []
                },
                type: {
                    type: Number,
                    default: 1
                }
            },
            data() {
                return {}
            },
            computed: {
                strong() {
                    let color = this.type * 1.5;
                    if (color >= 10) color = '#aaa';
                    else color = `#${color}${color}${color}`;
                    return {
                        'font-Size': (36 - 3 * this.type) + 'px',
                        'color': color,
                        'text-indent': (this.type - 1) + 'em'
                    }
                },
                content() {
                    return {
                        'font-Size': '16px',
                        'color': '#3e3e3e',
                    }
                }
            },
            mounted() {
                console.log(this.type);
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值