Angular2。如何访问所有级别的routeConfig元素表单指令

时间:2016-03-21 08:54:08

标签: angularjs angularjs-directive

我想创建一个指令,通过扩展 routerData

来检查在routeConfig元素上定义的ACL

如何访问routerConfig中定义的routerData?(必须具有所有级别的菜单)

示例:

我的ACL指令

@Directive({
    selector: '[myAcl]'
})
export class MyAclDirective {

    @Input('routerLink')
    routeParams: any[];

    /**
     * 
     */
    constructor(private _elementRef: ElementRef, private router:Router) {
    }

    ngAfterViewInit(){

        //Key name to find in 'routeConfig' acl definition of 'routerData'
        let componentAliasName = this.routeParams;

        //NEED TO GET ACCESS TO ROUTECONFIG DEFINITION
        //
        //Example: 
        //  @RouteConfig([
        //      { path:'/user/...',   name: 'UserLink', component: UserComponent, data: {res: 'user', priv: 'show'} } ])
        // AND GET BY 'name' == 'componentAliasName' my extend acl definition "data: {res: 'user', priv: 'show'}"

        console.log("CHECK ACL: " + data.res + " | " + data.priv);

        //ACL service 
        let hasAccess = AclService.checkAccess(data.res, data.priv);

        if (!hasAccess) {
            let el : HTMLElement = this._elementRef.nativeElement;
            el.parentNode.removeChild(el);   
        }


    }
}

MainComponent

@RouteConfig([
    { path:'/home',       name: 'HomeLink', component: HomeComponent, useAsDefault: true },
    { path:'/user/...',   name: 'UserLink', component: UserComponent, data: {res: 'user', priv: 'show'} }
])

@Component({
    selector: 'main-app',
    template: `
        <ul>
            <li><a myAcl [routerLink]="['HomeLink']">Home</a></li>
            <li><a myAcl [routerLink]="['UserLink']">User</a>
                <ul>
                    <li><a myAcl [routerLink]="['ProfileLink']">Profile</a>
                        <ul>
                            <li><a myAcl [routerLink]="['SubProfileLink']">SubProfile</a>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
        <router-outlet></router-outlet>
    `,
    directives: [ MY_ACL_DIRECTIVES, ROUTER_DIRECTIVES ],
})
export class MainComponent {

}

UserComponent

@RouteConfig([
    { path: '/profile/...', name: 'ProfileLink', component: ProfileComponent, data: {res: 'profile', priv: 'show'} }
])
@Component({
    selector: 'user-id',
    template: `<h1>User</h1>`,
    directives: [ROUTER_DIRECTIVES, MY_ACL_DIRECTIVES]
})
export class UserComponent {
}

ProfileComponent

@RouteConfig([
    { path: '/subprofile', name: 'SubProfileLink', component: SubProfileComponent, data: {res: 'profile', priv: 'details'} }
])
@Component({
    selector: 'profile-id',
    template: `<h1>Profile</h1>`,
    directives: [ROUTER_DIRECTIVES, MY_ACL_DIRECTIVES]
})
export class ProfileComponent {
}

SubProfileComponent

@Component({
    selector: 'subprofile-id',
    template: `<h1>Sub Profile</h1>`,
    directives: [ROUTER_DIRECTIVES, MY_ACL_DIRECTIVES]
})
export class SubProfileComponent {
}

Related link

1 个答案:

答案 0 :(得分:0)

您应该可以使用CanActivate装饰器。它看起来像这样:

@Component({...})
@CanActivate((next, prev) => (hasAccess))
export class UserComponent {
    // your component code
}

您可以在此处阅读更多内容:https://angular.io/docs/ts/latest/api/router/CanActivate-decorator.html