Answer the question
In order to leave comments, you need to log in
How to check route path dynamically in Angular 5?
There is a date/:date type route in which date, respectively, must be a date like 12/20/2018. The first problem is that the router does not accept dots in the date and when the corresponding route is overloaded, it says "Cannot GET /date/12/20/2018"?, and the second is that I need to dynamically check whether this date is correct and if not, then replace it with correct, how to do this?
Answer the question
In order to leave comments, you need to log in
To get started, you can change the date format and use as a separator, for example, a hyphen (20-12-2018).
It is solved as follows:
1) in the component's constructor, private _activatedRoute: ActivatedRoute
and private _router: Router
2) subscribe (preferably in ngOnInit) to ActivatedRoute and process the value of the route parameter:
this._activatedRoute.paramMap.subscribe((params: ParamMap) => {
let date: string = params.get('date');
if (/*если дата неправильна*/) {
date = ...; // изменим дату на корректную
this._router.navigate(['/date', date]);
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question