我有一个奇怪的问题,不明白如何解决这个问题。所以我有服务,检查你是否登录进入网络应用程序中的某个网址。如果没有,它会重定向到主页。
我的服务
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);
路由器可以激活module.routing
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);
但是当你重定向到主页时它会给我一个空白页。
答案 0 :(得分:0)
所以如果您遇到此问题请尝试此RouterModule.forRoot(appRoutes,{useHash:true})