Reputation: 53
If you have a route in angular such as:
{
path: ':state',
component: MyComponent,
},
Is there a way to have the path match only if the param's value for :state is in a list, ['CA','NV']?
Upvotes: 0
Views: 540
Reputation: 12960
You could setup a guard to do that:
{
path: ':state',
canActivate: [StateGuard],
component: MyComponent,
},
Guard
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);
}
You can't configure anything directly in your routing module that would handle this logic. Guards are a great use case for this kind of thing.
https://angular.io/guide/router#milestone-5-route-guards
You could also look into NavigationResolvers, but kind of a different use case.
Upvotes: 1