【问题标题】:Angular2. How to get access to the all level routeConfig elements form directives角2。如何访问所有级别的 routeConfig 元素表单指令
【发布时间】:2016-07-07 16:41:36
【问题描述】:

我想创建一个指令,它应该通过扩展 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);   
        }


    }
}

主组件

@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 {

}

用户组件

@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 {
}

配置文件组件

@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 {
}

子配置文件组件

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

Related link

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,我现在知道如何使用 canAcivate,但我不想只检查我的组件的访问权限,但必须检查与组件中相同的规则,例如菜单链接(菜单中没有渲染器链接)、按钮链接和其他安全组件的链接。我想在 routerData 中的一个位置定义组件和链接的扩展访问规则如果您在 routerData 中看到我的示例代码,我会在 canActivate 中定义资源和权限以检查访问相同的方法(请参阅我的下一篇文章)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2013-10-26
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多