[英]Angular 2 navigate to root from CanActivate service
嗨,我有一个奇怪的问题,不知道如何解决。 因此,我有服务来检查您是否在访问Web应用程序中的某些URL时登录。 如果不是,它将重定向到主页。
我的服务
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable()
export class AuthService implements CanActivate {
private userLogedin: string = localStorage.getItem('AUTHENTICATION');
constructor(private router: Router) {}
canActivate() {
if(this.userLogedin === 'true') {
return true;
}
this.router.navigateByUrl('/');
return false;
}
}
我的模块路由
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { LandingPageComponent } from 'app/components/LandingPageComponent/LandingPageComponent';
const landingPageRoutes: Routes = [
{path: '', component: LandingPageComponent, pathMatch: 'full'}
];
export const landingPageRouting: ModuleWithProviders = RouterModule.forChild(landingPageRoutes);
路由器可以激活模块路由
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { ProfilePageComponent } from 'app/components/ProfilePageComponent/ProfilePageComponent';
import { AuthService } from 'app/shared/AuthService';
const profilePageRoutes: Routes = [
{path: 'profile', component: ProfilePageComponent, canActivate: [AuthService] }
];
export const profilePageRouting: ModuleWithProviders = RouterModule.forChild(profilePageRoutes);
但是,当您重定向到主页时,它给了我一个空白页。
因此,如果您有此问题,请尝试使用RouterModule.forRoot(appRoutes,{useHash:true})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.