2017-09-05 81 views
0

我想建立的角應用程序,其具有正常的註冊/登陸表單和一些不同的ALT頁。角4 - 視圖設置

我的問題是,我沒有有任何想法如何控制不同的看法。例如,如果你用普通用戶類型登錄就可以看到圖1

如果你看到比其他不同的看法其它用戶類型登錄。

+1

請提供你的代碼。 –

+1

這是一個頁面,你應該開始:https://angular.io/guide/router。關鍵字:「路由」 –

回答

1

您可以重定向到一個警衛這樣的具體看法:

使用CanActivateChild保護children: []組件在你的路由文件,否則使用接口CanActivate。應用的邏輯保持不變。

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; 
    } 
    } 

}