Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating to the same route not refreshing the component?

I've upgraded to use the new Angular 2 router from router-deprecated and some behavior has changed.

The page I'm having issues with is a search page. We've chosen to put all the search terms in the URL using the HashLocationStrategy. The routes look like this:

const routes: RouterConfig = [   { path: '', component: HomeComponent },   { path: 'search/:term/:cat/:page/:sort/:size/:more', component: HomeComponent },   { path: 'search/:term/:cat/:page/:sort/:size', component: HomeComponent },   { path: 'search/:term/:cat/:page/:sort', component: HomeComponent },   { path: 'search/:term/:cat/:page', component: HomeComponent },   { path: 'search/:term/:cat', component: HomeComponent },   { path: 'search/:term', component: HomeComponent },   { path: 'details/:accountNumber', component: DetailsComponent } ]; 

I know a query string approach might be better fit for all these options but project requirements decided by other people...

Anyway if I navigate to localhost/#/search/Hello using this.router.navigate(['/search/Hello']); then the router works fine and everything is great. While on that page if I try to this.router.navigate(['/search/World']); then the URL in the browser's address bar will update accordingly but the component doesn't change at all.

Previously I could use routerCanReuse to indicate that I did want to reuse the search component and routerOnReuse would rerun the search when the navigation happened. I don't see equivalent's to these in @angular/router. How can I reload the current route with new URL parameters?

I'm running version 2.0.0-rc.3 for Angular 2 and 3.0.0-alpha.8 of @angular/router.

like image 318
Corey Ogburn Avatar asked Jun 30 '16 15:06

Corey Ogburn


People also ask

Does router navigate refresh page?

All it does is store current url then navigates to the application root and back to current url forcing a reload.

Does the page reload when we navigate to another route react?

react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.

How do I reload components in angular 9?

There is button by which i am updating my page data. on button click i have save data in back end and refresh data. So to reload/refresh the component - as per my requirement - is only to refresh the data. if this is also your requirement then use the same code written in constructor of component.

How do you reload the current component in angular 6?

Angular will reload page which means Page Component/Resolvers/Guards in specific way. using this. router. onSameUrlNavigation = 'reload';


1 Answers

If only the params has changes the component itself won't be initialize again. But you can subscribe to changes in the parameters that you send.

For example on ngOnInit method you can do something like this:

ngOnInit() {     this.sub = this.route.params.subscribe(params => {        const term = params['term'];        this.service.get(term).then(result => { console.log(result); });      });   } 
like image 98
Doron Brikman Avatar answered Sep 22 '22 14:09

Doron Brikman