简体   繁体   中英

How to reload a view after router.navigate in Angular 4?

I have a switch to shop function which is controlled by a header component and displays a dashboard for that venue.

In that header component, I navigate to dashboard route, even if I already in that route.

this.router.navigate(['/venues', this.currentVenue._id, 'dashboard']);

But it doesn't trigger ngOnInit hook again so my view is not reloaded.

What is the correct workaround for such case?

Do you need to reload the view? Or just reget some data?

If you only need to reget data based on the routing parameter, you can do this:

ngOnInit(): void {
    this.route.params.subscribe(
        params => {
            const id = +params['id'];
            this.getMovie(id);   // <- Or whatever you need to do here
        }
    );
}

In the ngOnInit, this code subscribes to the route parameters. So every time the parameters change, even if the view is not changed/reloaded, the change will be picked up as part of the subscription and the associated callback is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM