Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 346
Er Vipin Sharma Avatar asked Aug 25 '17 05:08

Er Vipin Sharma


People also ask

How do you reload the component of the same URL in angular 6?

In order to reload routed components on same url navigation, you need to set onSameUrlNavigation to 'reload' and provide a RouteReuseStrategy which returns false for shouldReuseRoute .

What is Route reuse strategy in angular?

BaseRouteReuseStrategylink This base route reuse strategy only reuses routes when the matched router configs are identical. This prevents components from being destroyed and recreated when just the route parameters, query parameters or fragment change (that is, the existing component is reused).

How do you refresh a component from another component in angular 9?

Clicking on the Reload Page button,will execute the constructor() and ngOnInit() of the AppComponent,ChildComponent and TestComponent again. 3. Clicking on the Reload Test Component button, we will navigate to the /test route and execute the constructor() and ngOnInit() of the TestComponent again.

What is Route reload ()?

Using reload() method: Angular route service reload() method is used when we want just the current route to be reloaded instead of making our entire application reloading or refreshing.


1 Answers

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

});
like image 134
Pardeep Jain Avatar answered Sep 29 '22 07:09

Pardeep Jain