Angular Router无法重定向true组件

时间:2019-06-04 17:49:11

标签: angular angular-ui-router

我有2个路由模块: app-routing landlord-routing ,并且我有 dashboard组件将每个角色重定向到其组件。当我添加 NotFouldComponent 时,它将始终显示此404页,但是当我注释此行(NotFouldComponent)时,它可以工作,我可以重定向到真正的组件。您能帮我解决此错误吗?

在应用路由模块中:

const routes: Routes = [
    { path: '',   redirectTo: '/dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: DashboardComponent },
    { path: '**', component: NotFoundComponent}
];

在房东路由模块中:

const LandlordChildRouters: Routes = [
    {
        //path: 'landlord/dashboard', component: LandlordComponent 
        path: 'landlord',
        component: LandlordComponent,
        canActivate: [AuthGuard], data: { roles: [Role.Lanlord] },
        children: [
            {
                path: 'dashboard',
                component: LandlordDashboardComponent
            },
            {
                path: 'profile',
                component: LandlordProfileComponent
            }
        ]
    }
];

在dashboardComponent中获取用户角色,然后重定向:

get isLandlord() {
    return (this.currentUser && this.currentUser.role === Role.Lanlord);
}

get isTenant() {
    return (this.currentUser && this.currentUser.role === Role.Tenant);
}

deleteDataInLocal() {
    this.authenticationService.logout();
}

ngOnInit() {
    if(this.isLandlord){
        this.router.navigate(['/landlord/dashboard']);
    }
    else if(this.isTenant){
        this.router.navigate(['/tenant/dashboard']);
    }
    else{
        console.log("delete")
        this.deleteDataInLocal();
        this.router.navigate(['/login']);
    }
}

1 个答案:

答案 0 :(得分:0)

试试看是否有效

{ path: '**', redirectTo: '/notfound', pathMatch: 'full' },
{ path: 'notfound', component: NotFoundComponent, pathMatch: 'full' }