2016-09-13 3 views
0

ルータ3.0.0-rc.1で角度2を使用しています。チュートリアルから次のテンプレート考えてみましょう:「私たちは、ユーザがログインするまで、リンクを隠すことができしかし、それは難しいし、維持するのが難しいのです。。」:ややunhelpfully角度2のルータの経路を隠すにはどうすればいいですか?

` 
<nav> 
    <a routerLink="/crisis-center" routerLinkActive="active" 
     [routerLinkActiveOptions]="{ exact: true }">Crisis Center</a> 
    <a routerLink="/heroes" routerLinkActive="active">Heroes</a> 
    <a routerLink="/crisis-center/admin" routerLinkActive="active">Crisis Admin</a> 
</nav> 
<router-outlet></router-outlet> 
` 

チュートリアルが追加されます

ユーザーがログインするまで管理リンクを非表示にしたいと思います(このタブを無効にしてログインする必要はありません。タブはそこにあります)。ルーティングでそれを指定する簡単な方法はありますか?

答えて

0

リンクの表示/非表示を信頼できません。 canActivate機能を確認してください。指定したルートにアクセスできるかどうかを指定できます。完全にここにカバーされてhttp://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html。リンクを隠し

示すものであり/

import { Directive, OnInit, TemplateRef, ViewContainerRef } from '@angular/core'; 

@Directive({ 
    selector: '[yourDirectiveSelector]' 
}) 
export class YourDirective implements OnInit { 

    private customAuthService: CustomAuthService; 
    private templateRef: TemplateRef<any>; 
    private viewContainerRef: ViewContainerRef; 


    constructor(customAuthService: customAuthService, templateRef: TemplateRef<any>, viewContainerRef: ViewContainerRef) { 
    this.customAuthService = customAuthService; 
    this.templateRef = templateRef; 
    this.viewContainerRef = viewContainerRef; 
    } 

    ngOnInit() { 
    if (this.customAuthService.isOk()) { 
     this.viewContainerRef.createEmbeddedView(this.templateRef); 
    } else { 
     this.viewContainerRef.clear(); 
    } 
    } 
} 
例えば、役割とディレクティブを持つことになり、いくつかのAuthServiceによって行うことができます
関連する問題