Angular 2 Checking route parameters

How to check route parameters in Angular2? For example, I have a route:

{path: 'artist/:id', component: ArtistInfoComponent} 

All I want to do is check if the id parameter is a valid number, and if so, just show the component. Otherwise, display the error message page.

I found Route Guard in Angular 2, but I think that this is not the best way to do validation, because I can have many different parameters for different pages and to write a separate Route Guard for all these pages is not what I want,

Is it possible to specify a regular expression for each route parameter?

UPD: Maybe there is a way to do a check on the request parameters? I mean, if I have this url: /search?q=blablabla&page=2 , how can I check if the page parameter is valid? Is there any way to do this without a guard?

+6
source share
1 answer

Guardsmen are the way to go, you can put the verification configuration in the data section of the route and create a reusable defender to check the parameters based on this configuration.

+2
source

Source: https://habr.com/ru/post/1013509/


All Articles