2017-06-18 44 views
1

我正在處理一個簡單的登錄過程,我嘗試保護某些路徑,除非它們已通過身份驗證。Angular - router.navigate()不重定向到目標頁面

app.routing.ts

const appRoutes: Routes = [ 
    { 
     path: 'add-merchant-admin', 
     component : AddMerchantAdminComponent, 
     canActivate : [AuthGard] 
    }, 
    { 
     path: 'list-merchant-admin', 
     component : ListMerchantAdminComponent, 
     canActivate : [AuthGard] 
    }, 
    { 
     path: 'login', 
     component : LoginComponent 
    }, 
    { 
     path: '**', 
     component: NotFoundComponent 
    } 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

authGard.service.ts

import { Injectable } from '@angular/core'; 
    import {CanActivate, Router} from "@angular/router"; 
    import {AuthenticationService} from "../authentication-service/authentication.service"; 

    @Injectable() 
    export class AuthGard implements CanActivate { 

    constructor(private _authService:AuthenticationService, private _router:Router) { } 

    canActivate() { 
     if(this._authService.isLoggedIn) 
     return true; 

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

認證服務

@Injectable() 
    export class AuthenticationService { 

     isLoggedIn = false; 

     constructor() { 
     } 

     login(){ 
     this.isLoggedIn = true; 
     } 

     logout(){ 
     this.isLoggedIn = false; 
     } 
    } 

瓦我嘗試訪問一個保護路徑add-merchant-admin,瀏覽器不斷加載頁面,直到它凍結消耗大量內存。

這些是關於我的應用程序的詳細信息。

節點:6.10.2

OS:win32的64

@角/動畫:4.2.3

@角/共同:4.2.3

@ angular/compiler:4.2.3

@角/芯:4.2.3

@角/形式:4.2.3

@角/ HTTP:4.2.3

@角/材料:2.0.0-beta.6

@角/平臺的瀏覽器:4.2.3

@角/平臺的瀏覽器的動態:4.2.3

@角/路由器:4.2.3

@角/ CLI:1.0.1

@角/編譯-CLI:4.2.3

依賴注入被驗證。

組件被正確導入。

我不知道這是怎麼回事這個程序,通常它應該工作。

希望你們能幫助我。

巨大的預先感謝。

回答

2

修改如下面的路線,

const appRoutes: Routes = [ 
    { 
     path: 'add-merchant-admin', 
     component : AddMerchantAdminComponent, 
     canActivate : [AuthGard] 
    }, 
    { 
     path: 'list-merchant-admin', 
     component : ListMerchantAdminComponent, 
     canActivate : [AuthGard] 
    }, 
    { 
     path: 'login', 
     component : LoginComponent 
    }, 
    { 
     path: 'notfound', 
     component :NotFoundComponent 
    }, 
    { 
     path: '', 
     redirectTo: 'login', 
     pathMatch: 'full' 
    }, 
    { 
     path: '**', 
     redirectTo: 'notfound', 
     pathMatch: 'full' 
    }, 
]; 
+0

您爲什麼認爲問題與路線的定義有關? –

+0

默認情況下,您正在擁有該路線定義的路徑爲空。 – Aravind

+0

是的,但那會導致應用程序始終呈現NotFoundComponent在第一次加載,而不是在應用程序凍結 –

0

更改AuthGuard這樣:

import { Injectable } from '@angular/core'; 
    import {CanActivate, Router} from "@angular/router"; 
    import {AuthenticationService} from "../authentication-service/authentication.service"; 

    @Injectable() 
    export class AuthGard implements CanActivate { 

    constructor(private _authService:AuthenticationService, private _router:Router) { } 

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { 
     if(this._authService.isLoggedIn) 
     return true; 

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

隨着/作爲參數陣列的導航方法的第一個參數前綴你告訴角度即路徑是absoluth(從根開始)。