Angular 如何根据条件重定向角4路由
根据用户类型,当路径为空时,我必须重定向到页面。因此,我尝试了以下方法Angular 如何根据条件重定向角4路由,angular,redirect,conditional,angular4-router,Angular,Redirect,Conditional,Angular4 Router,根据用户类型,当路径为空时,我必须重定向到页面。因此,我尝试了以下方法 export const Approutes: Routes = [ { path: '', pathMatch: 'full'}, { path: 'locals', component: LaunchScreenComponent, children: [ { path: 'home',
export const Approutes: Routes = [
{ path: '', pathMatch: 'full'},
{
path: 'locals', component: LaunchScreenComponent,
children: [
{
path: 'home',
component: HomeScreenComponent,
},
{
path: 'change-password',
component: ChangePasswordComponent
}
]
}
];
if(Approutes[0].path.length == 0){
if(sessionStorage.authToken){
if(sessionStorage.user1 == 'true'){
Approutes[0].redirectTo = '/user1/manage';
} else if(!sessionStorage.user1 && sessionStorage.user2 == 'false'){
Approutes[0].redirectTo = '/user2/listing';
} else{
Approutes[0].redirectTo = '/locals/home';
}
} else {
Approutes[0].redirectTo = '/locals/home';
}
}
成功了。但是,当我为prod构建时,它会给出错误。错误是:应该有重定向到或组件bla-bla中的任何一个。。
有人能帮我吗。您是否尝试过:
export const Approutes: Routes = [
{
path: 'locals', component: LaunchScreenComponent,
children: [
{
path: 'home',
component: HomeScreenComponent,
},
{
path: 'change-password',
component: ChangePasswordComponent
}
]
}
];
if(sessionStorage.authToken){
if(sessionStorage.user1 == 'true'){
Approutes.unshift({ path: '', pathMatch: 'full', redirectTo: '/user1/manage'});
} else if(!sessionStorage.user1 && sessionStorage.user2 == 'false') {
Approutes.unshift({ path: '', pathMatch: 'full', redirectTo: '/user2/listing'});
} else{
Approutes.unshift({ path: '', pathMatch: 'full', redirectTo: '/locals/home'});
}
} else {
Approutes.unshift({ path: '', pathMatch: 'full', redirectTo: '/locals/home'});
}
不会影响您的流程,并将消除
prod
错误 这么简单的技术。我没有试过。谢谢你的回答。这是实现这一目标的正确途径吗?只是想知道是的,这是一个很好的方法。如果您是在resolve中创建的,那么您将必须在所需路径上创建新的导航。您好,感谢您的回复。上述错误已解决,但它提供了null的无法读取属性参数。你能告诉我错误是什么吗请检查我上传的图片你能让代码在git或stackblitz上可用吗?