2017-05-22 17 views
3

登录我有这样的路线:角2重定向用户是否在

const routes: Routes = [ 
    { path: '', redirectTo: '/login', pathMatch: 'full' }, 
    { path: 'login', component: LoginComponent }, 
    { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }, 
]; 

AuthGuard

export class AuthGuard implements CanActivate, CanActivateChild { 
    constructor(private router: Router, 
       private authService: AuthService) { } 

    canActivate() { 
    if (this.authService.isLoggedIn()) { 
     return true; 
    } 
    this.router.navigate(['login']); 
    return false; 
    } 
} 

当用户访问该网站时,他被重定向到登录页面。当用户尝试访问/dashboard路由而没有验证时也会发生同样的情况。如果他已登录,如何将用户重定向到/dashboard?例如,当我访问myapp.com并且我已登录时,我想将其重定向到myapp.com/dashboard

+0

上'canActivate'将无法正常工作这样做呢? 'if(this.authService.isLoggedIn()){this.router.navigate(['dashboard']);返回true; }'。 – developer033

+0

@ developer033,我试过了,它不起作用!会有一个无限循环。 –

+0

嗯,所以你只能在你的'LoginComponent'(在'ngOnInit')上检查它.. – developer033

回答

2

在这种情况下,我将一切可能重定向到DashboardComponent使用

{path: '', redirectTo: 'dashboard', pathMatch: 'full'}

和比仪表板部件,如果用户登录或不能够确定的(因为它具有AuthGuard活性),并且如果用户没有登录它会重定向他登录。

+0

谢谢,我认为它会是对的! –

+0

尽管这样做有效,但它会倒退,只要有人点击您的主页,就会迫使您进入“信息中心”流程。使用'resovle'的另一个答案更加灵活,并且避免了重定向到全部位置('/'进入'/ login'或'/ dashboard'而不是'/ - >/dashboard - >/login' –

+0

容易和非常聪明:) thx – Whisher

6

您想查看Route界面中的resolve属性。 https://angular.io/docs/ts/latest/api/router/index/Resolve-interface.html

以下属性的所有可用于路线:

export interface Route { 
    path?: string; 
    pathMatch?: string; 
    matcher?: UrlMatcher; 
    component?: Type<any>; 
    redirectTo?: string; 
    outlet?: string; 
    canActivate?: any[]; 
    canActivateChild?: any[]; 
    canDeactivate?: any[]; 
    canLoad?: any[]; 
    data?: Data; 
    resolve?: ResolveData; 
    children?: Routes; 
    loadChildren?: LoadChildren; 
    runGuardsAndResolvers?: RunGuardsAndResolvers; 
    _loadedConfig?: LoadedRouterConfig; 
} 

resolve设计,让您之前程序的路径加载某些类型的数据。对于resolve签名看起来是这样的:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<T>|Promise<T>|T 

我们可以但是忽略了签名,只是覆盖它,因为在这种情况下,我们只是想重定向到/仪表板如果用户-logged IN-。否则显示/登录路由正常。

解决方法是创建一个injectable类并将其附加到登录路由的resolve属性。

@Injectable() 
class IsLoggedIn { 
    constructor(
    private router: Router, 
    private authService: AuthService) { 
    } 

    resolve(): void { 
    if (this.authService.isAuthenticated) this.router.navigate(['/dashboard']) 
    } 
} 

在您/登录路线添加resolve属性。

{ 
    path: 'login', 
    component: LoginComponent, 
    resolve: [IsLoggedIn] 
} 

现在,只要-logged在 - 用户试图去/登录,他们会被重定向到/仪表板没有看到登录页面。

+1

这个方法重定向确定,但仍然加载模块! – BenRacicot

0

可能最快的事情是调整您的LoginComponent

鉴于this.authService.isLoggedIn()返回一个布尔值。在你LoginComponent您可以快速检查状态ngOnInit如下:

import { Component, OnInit } from '@angular/core'; 
import { Router, ActivatedRoute } from '@angular/router'; 

import {AuthService} from '<path_to_your_auth_service>'; 

@Component({ 
    selector: 'app-login', 
    templateUrl: './login.component.html', 
    styleUrls: ['./login.component.scss'], 
    providers: [] 
}) 
export class LoginComponent implements OnInit { 
    constructor(
    private _route: ActivatedRoute, 
    private _router: Router, 
    private _authService: AuthService 
) 

    ngOnInit() { 
    // get return url from route parameters or default to '/' 
    this.returnUrl = this._route.snapshot.queryParams['returnUrl'] || '/'; 

    // CHECK THE isLoggedIn STATE HERE 
    if (this._authService.isLoggedIn()) { 
     this._router.navigate([this.returnUrl]); 
    } 

    } 

    // Rest fo the logic here like login etc 

    login() { } 

} 

请注意,您只需要添加这ngOnInit

if (this._authService.isLoggedIn()) { 
    this._router.navigate([this.returnUrl]); 
}