Angular 角度路由模块中如何按条件重定向通配符路由
当用户使用通配符键入错误的URL但按条件控制时,是否有办法处理重定向路由 我的项目分别有3个主要模块,每个模块都有一些只能由用户角色访问的条件 问题是我不知道如何处理它的条件,我已经尝试过激活,但它不工作 main.ts:14错误:路由“**”的配置无效。必须提供以下选项之一:组件、重定向到、子项或加载子项 这里是我的一些代码Angular 角度路由模块中如何按条件重定向通配符路由,angular,typescript,angular8,angular-routing,Angular,Typescript,Angular8,Angular Routing,当用户使用通配符键入错误的URL但按条件控制时,是否有办法处理重定向路由 我的项目分别有3个主要模块,每个模块都有一些只能由用户角色访问的条件 问题是我不知道如何处理它的条件,我已经尝试过激活,但它不工作 main.ts:14错误:路由“**”的配置无效。必须提供以下选项之一:组件、重定向到、子项或加载子项 这里是我的一些代码 const routes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'cust
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'customer1'
},
{
path: 'customer1',
component: RootOneComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeOneComponent
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
{
path: 'customer2',
component: RootTwoComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeTwoComponent
},
{
path: 'BlogPicture',
loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule)
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
{
path: 'customer3',
component: RootThreeComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeThreeComponent
},
{
path: 'blog-price',
loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule)
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
// wild card condition is there the way to manage this?
{
path: '**',
redirectTo: 'classic'
}
]) 在你的app.routing.module.ts中
const routes: Routes = [{
path: "customer1",
component: FirstCustomerModule,
pathMatch: "full"
},
{
path: "customer2",
data: { preload: true },
loadChildren: () => SecondCustomerModule
},
{
path: "customer3",
data: { preload: true },
loadChildren: () => ThirdCustomerModule
}
}
路径定义了您提到的路径,在模块中,您必须提到提到的模块将被命中。然后在你的模块中,你必须相对地提到路径,我刚才提到的只是应用程序级路由,然后你可以进一步实现模块级路由,加载子对象告诉我们的应用程序它是一个模块,它有更多的组件对于条件使用这个:if(condition){this.router.navigate([“/无论什么]);}哦,是的,我忘记在Angular v.8I之后更新我的代码了。我刚刚更新了我的代码,请您再看一看好吗?使用组件替换末尾的redirectTo如果您想使用redirect,请添加pathMatch=“full”