• 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源码:点击这里

  • 相关阅读:
    Homebrew简介及安装
    MongoDB MapReduce学习笔记
    mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
    【资源共享】《Rockchip 量产烧录 指南 V1.0》
    【资源共享】《DDR常见问题简单排查》
    【技术案例】Firefly-RK3399多路视频编解码
    【资源共享】《Rockchip 以太网 开发指南 V2.3.1》
    【资源共享】Rockchip Audio 开发指南
    关于在RK3288上安装Opencv的方法
    debian stretch + kernel 4.4 固件发布(支持硬件加速),可安装kodi
  • 原文地址:https://www.cnblogs.com/LongJiangXie/p/5756312.html
Copyright © 2020-2023  润新知