angular2 递归导航菜单实现方式

看了网上很多源码,基本都是采用循环三级的方式。如果是无限级的菜单,就无法实现了。

 

菜单格式:

 [
            {
                "title": "Item-1",
                "iconClass": "fa fa fa-flask",
                "link": "#1",
                "notice": 0,
                "subMenus": null
            },
            {
                "title": "Item-2", "iconClass": "fa fa-level-down", "link": null, "notice": 0,
                "subMenus": [
                    {
                        "title": "Item-2-1",
                        "iconClass": "fa fa fa-flask",
                        "link": "#2",
                        "notice": 0,
                        "subMenus": null
                    }, {
                        "title": "Item-2-2",
                        "iconClass": "fa fa fa-flask",
                        "link": "#3",
                        "notice": 0,
                        "subMenus": null
                    }]
            },
            {
                "title": "Item-3", "iconClass": "fa fa-level-down", "link": null, "notice": 4,
                "subMenus": [
                    {
                        "title": "Item-3-1",
                        "iconClass": "fa fa fa-flask",
                        "link": "#4",
                        "notice": 1,
                        "subMenus": null
                    },
                    {
                        "title": "Item-3-2",
                        "iconClass": "fa fa fa-flask",
                        "link": null,
                        "notice": 3,
                        "subMenus": [
                            {
                                "title": "Item-3-2-1",
                                "iconClass": "fa fa fa-flask",
                                "link": "#6",
                                "notice": 1,
                                "subMenus": null
                            },
                            {
                                "title": "Item-3-2-2",
                                "iconClass": "fa fa fa-flask",
                                "link": "#7",
                                "notice": 2,
                                "subMenus": [
                                    {
                                        "title": "Item-4-2-1",
                                        "iconClass": "fa fa fa-flask",
                                        "link": "#6",
                                        "notice": 1,
                                        "subMenus": null
                                    },
                                    {
                                        "title": "Item-4-2-2",
                                        "iconClass": "fa fa fa-flask",
                                        "link": "#7",
                                        "notice": 2,
                                        "subMenus": [
                                            {
                                                "title": "Item-5-2-1",
                                                "iconClass": "fa fa fa-flask",
                                                "link": "#6",
                                                "notice": 1,
                                                "subMenus": null
                                            },
                                            {
                                                "title": "Item-5-2-2",
                                                "iconClass": "fa fa fa-flask",
                                                "link": "#7",
                                                "notice": 2,
                                                "subMenus": null
                                            }]
                                    }]
                            }]
                    }]
            }
        ];

 

AppComponent.ts代码:

import { Component } from '@angular/core';
import {TreeViewComponent} from './treeview/treeview.component';
import {MenuItem} from './treeview/menuItem';;
@Component({
    selector: 'my-app',
    template: '<ul tree-view [directories]="directories"></ul>',
    directives: [TreeViewComponent]
})
export class AppComponent {
    directories: MenuItem[];
    constructor() {
        this.directories = [
            {
                "title": "Item-1",
                "iconClass": "fa fa fa-flask",
                "link": "#1",
                "notice": 0,
                "subMenus": null
            },
            {
                "title": "Item-2", "iconClass": "fa fa-level-down", "link": null, "notice": 0,
                "subMenus": [
                    {
                        "title": "Item-2-1",
                        "iconClass": "fa fa fa-flask",
                        "link": "#2",
                        "notice": 0,
                        "subMenus": null
                    }, {
                        "title": "Item-2-2",
                        "iconClass": "fa fa fa-flask",
                        "link": "#3",
                        "notice": 0,
                        "subMenus": null
                    }]
            },
            {
                "title": "Item-3", "iconClass": "fa fa-level-down", "link": null, "notice": 4,
                "subMenus": [
                    {
                        "title": "Item-3-1",
                        "iconClass": "fa fa fa-flask",
                        "link": "#4",
                        "notice": 1,
                        "subMenus": null
                    },
                    {
                        "title": "Item-3-2",
                        "iconClass": "fa fa fa-flask",
                        "link": null,
                        "notice": 3,
                        "subMenus": [
                            {
                                "title": "Item-3-2-1",
                                "iconClass": "fa fa fa-flask",
                                "link": "#6",
                                "notice": 1,
                                "subMenus": null
                            },
                            {
                                "title": "Item-3-2-2",
                                "iconClass": "fa fa fa-flask",
                                "link": "#7",
                                "notice": 2,
                                "subMenus": [
                                    {
                                        "title": "Item-4-2-1",
                                        "iconClass": "fa fa fa-flask",
                                        "link": "#6",
                                        "notice": 1,
                                        "subMenus": null
                                    },
                                    {
                                        "title": "Item-4-2-2",
                                        "iconClass": "fa fa fa-flask",
                                        "link": "#7",
                                        "notice": 2,
                                        "subMenus": [
                                            {
                                                "title": "Item-5-2-1",
                                                "iconClass": "fa fa fa-flask",
                                                "link": "#6",
                                                "notice": 1,
                                                "subMenus": null
                                            },
                                            {
                                                "title": "Item-5-2-2",
                                                "iconClass": "fa fa fa-flask",
                                                "link": "#7",
                                                "notice": 2,
                                                "subMenus": null
                                            }]
                                    }]
                            }]
                    }]
            }
        ];
    }
}

 这里有人也许有人会问 directives 指令描述了 TreeViewComponent 组件,为什么我的 template  里面没提供的 <tree-view></tree-view>之类的自定义标签。细心的朋友会发现 ul 里有 tree-view  。没错,这事Angular2的另一种组件选择方式写法。如果采用自定义标签的方式,那么在原有的样式中,可能因为代码中多了<tree-view></tree-view> 会导致原来的样式失效了。

 

例如:

<style>
    div > ul > li {
        color: #0094ff;
    }
    
    ...
</style>
<div>
    <ul>
        <li>
            ...
        </li>
    </ul>
</div>

<!-- 加入 <tree-view></tree-view> 后 -->


<div>
    <tree-view>
        <ul>
            <li>
                ...
            </li>
        </ul>
    </tree-view>
</div>

 

 app/treeview/treevieww.component.ts代码:

import { Component, OnInit, Input } from '@angular/core';
import {MenuItem} from './menuItem';

@Component({
    moduleId: module.id,
    selector: '[tree-view]',
    templateUrl: 'treeview.component.html',
    directives: [TreeViewComponent],
})
export class TreeViewComponent implements OnInit {
    @Input() directories: MenuItem[];

    constructor() { }

    ngOnInit() { }

}

 使用自定义标签的选择方式,那么selector 选择器就不需要加上 [ ... ] 中括号。

 

 

最后结果:

 

 

oschina源码:点击这里

转载于:https://www.cnblogs.com/LongJiangXie/p/5756312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值