D
D
Dmitry Gavrilenko2016-12-01 01:31:04
Angular
Dmitry Gavrilenko, 2016-12-01 01:31:04

Redirect in Angular 2?

Hello. There are 2 components. InfoComponent and DetailsComponent. There is also a standard AppComponent. I use routers.

{path: 'info', component: InfoComponent},
{path: 'details', component: DetailsComponent}

I need, under a certain condition, to redirect the user to a specific url.
Let's say the user opens site/details and is redirected to site/info , and that's because it's morning.
If(date.hour > 4 && date.hour  < 10) // <--- к примеру
router.navigate(['info'])

In NgOnInit this code does not work, in the constructor too. It only works in ngOnAfterViewInit if I'm not mistaken, but at this point, the details page of the component is already full on the screen. Where, to shove it, what was the transition, before the start of the "rendering" of the page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Kayun, 2016-12-01
@Kayun

Use canActivate https://angular.io/docs/ts/latest/guide/router.htm...
Check the condition, redirect to the correct url and return false.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question