import { Injectable , Inject , Optional , OnDestroy } from '@angular/core' ; import { MenuItem } from './menu-item' ; @ Injectable ( ) export class MenuService implements OnDestroy { private data : MenuItem [ ] = [ ] ; constructor ( ) { } set menus ( items ) { this . data = items ; } get menus ( ) { return this . data ; } /**
     * 清空菜单
     */ clear ( ) { this . data = [ ] ; } param url openedByUrl ( url ) { if ( ! url ) return ; } param url getPathByUrl ( url ) { let result = [ ] ; this . findMenuItem ( url , this . data , result ) ; result . reverse ( ) ; return result ; } private findMenuItem ( url , items , result ) { let item ; for ( var i = 0 ; i < items . length ; i ++ ) { item = items [ i ] ; if ( item . items && item . items . length ) { if ( this . findMenuItem ( url , item . items , result ) ) { result . push ( item ) ; return true ; } } if ( item . route == url || ( ! ( item . items && item . items . length ) && url . startsWith ( item . route ) ) ) { result . push ( item ) ; return true ; } } } ngOnDestroy ( ) { } }