2017-09-05 9 views
0

通常の登録/ログインフォームといくつかの異なるaltページを持つ角型アプリケーションを構築したいと思います。角4 - 表示設定

私の問題は、さまざまなビューをどのように制御するかわからないということです。たとえば、通常のユーザタイプでログインした場合、ビュー1が表示されます。

他のユーザタイプでログインすると、他のユーザタイプとは異なる表示が表示されます。

+1

コードを入力してください。 –

+1

https://angular.io/guide/routerを開始するページです。キーワード: "ルーティング" –

答えて

1

あなたは、このようなガードの特定のビューにリダイレクトすることができます。それ以外のインタフェースCanActivateを使用し、あなたのルーティングファイルでchildren: []コンポーネントを保護するため

使用CanActivateChild。適用される論理は同じままである。

import {Injectable} from '@angular/core'; 
import {Router, CanActivateChild} from '@angular/router'; 
import {AuthenticationService} from '../services'; 

@Injectable() 
export class LoginAuthGuard implements CanActivateChild { 

    constructor(private router: Router, 
       private authenticationService: AuthenticationService) { 
    } 

    canActivateChild(): boolean { 
    if (this.authenticationService.isLoggedIn()) { 
     if (**normal user type **) { 
     this.router.navigate(['/view1']); 
     } else { 
     this.router.navigate(['/view2']); 
     } 

     return true; 
    } else { 

     this.router.navigate(['/login']); 
     return false; 
    } 
    } 

} 
関連する問題