Angular 8 - 路由路径参数的值必须匹配字符串列表
Angular 8 - route path param's value must match list of strings
如果您在 angular 中有一条路线,例如:
{
path: ':state',
component: MyComponent,
},
有没有办法让路径仅在 :state 的参数值在列表中时才匹配,['CA','NV']?
你可以设置一个守卫来做到这一点:
{
path: ':state',
canActivate: [StateGuard],
component: MyComponent,
},
后卫
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot, redirectOnFailure = true): Observable<boolean> {
const providedState = next.paramMap.get('state');
if (['WA', 'CA'].includes(providedState.toUpperCase())) {
return of(true);
}
return of(false);
}
您不能在您的路由模块中直接配置任何可以处理此逻辑的内容。守卫是这类事情的一个很好的用例。
https://angular.io/guide/router#milestone-5-route-guards
您也可以查看 NavigationResolvers,但使用情况有所不同。
如果您在 angular 中有一条路线,例如:
{
path: ':state',
component: MyComponent,
},
有没有办法让路径仅在 :state 的参数值在列表中时才匹配,['CA','NV']?
你可以设置一个守卫来做到这一点:
{
path: ':state',
canActivate: [StateGuard],
component: MyComponent,
},
后卫
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot, redirectOnFailure = true): Observable<boolean> {
const providedState = next.paramMap.get('state');
if (['WA', 'CA'].includes(providedState.toUpperCase())) {
return of(true);
}
return of(false);
}
您不能在您的路由模块中直接配置任何可以处理此逻辑的内容。守卫是这类事情的一个很好的用例。
https://angular.io/guide/router#milestone-5-route-guards
您也可以查看 NavigationResolvers,但使用情况有所不同。