angular 2: Reload same component again when redirect on the same route
Asked Answered
V

3

6

I am using 'routerLink' to navigate my routes and it is working fine. But when I click again on the same button, I want that component will load again. But currently, it's not working in angular2.

app.component.html

<ul class="nav navbar-nav navbar-right">
            <li><a [routerLink]="['/events']" routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}">All Events</a></li>
            <li><a [routerLink]="['/events/new']" routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}">Create New</a></li>
</ul>

I want, When I click on 'All Events' tab, again and again, events component load every time.

How to achieve this scenario in angular2 ?

Vaginectomy answered 25/8, 2017 at 5:53 Comment(2)
why you want to load same component again and again ?Lipetsk
please look at this post its similar #47855740Ondrea
L
6

I want, When I click on 'All Events' tab, again and again, events component load every time.

This is Not Possible in angular2, reason is once you load any component that component is only reload once you navigate from another route to that route again.

But you can reload forcefully

Basically there are two methods which are

  • you can call ngOnInit() method again and again as per need, which is not a recommended way

  • you can call one commonn method from ngOnInit() and later as per need call that function again like this

    ngOnInit(){
        this.callingFunction();
    }
    
    forLaterUse(){
        this.callingFunction(); 
    }
    

Another way is if you want to load same component on param change you can use this in constructor

this.route.params.subscribe((params: Params) => {
    console.log(params); 
    this.callingFunction();
    // this will be called every time route changes
    // so you can perform your functionality here

});
Lipetsk answered 25/8, 2017 at 6:4 Comment(1)
this is different question just strikes now, Is there any chance of restricting to load component on navigating from other route to same route ?.Hydro
L
5

This github link below has the right solution https://github.com/angular/angular/issues/13831

Put the below code in the constructor of the component which you want to load

this.router.routeReuseStrategy.shouldReuseRoute = function () {
  return false;
};

this.router.events.subscribe((evt) => {
  if (evt instanceof NavigationEnd) {
    this.router.navigated = false;
    window.scrollTo(0, 0);
  }
});
Larisa answered 19/7, 2018 at 16:6 Comment(0)
T
1

See here: How to reload the current route with the angular 2 router

To summarize - its not a function that is built into the router. It sounds like the best option is using a service that will refresh or re-fetch any relevant data, and if anything changes, Angular will automatically update the view.

If someone says something is not possible in Angular, that is almost always not true. Its just not possible using currently existing solutions. ;-)

Tymothy answered 25/8, 2017 at 6:9 Comment(2)
If someone says something is not possible in Angular, that is almost always not true dude but according to scenario its not possible in angular's way.Lipetsk
Yes i was just being cheeky. :-p. You did provide solutions after allTymothy

© 2022 - 2024 — McMap. All rights reserved.