嗨,我有奇怪的问题,不知道如何解决该问题。因此,我有服务可以检查您是否登录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);
路由器Canactivate 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);
但是,当hi重定向到主页时,它给了我一个空白页。
因此,如果您有此问题,请尝试使用此routermot.forroot(批准,{usehash:true})