Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 5.2 onSameUrlNavigation not working

I'm trying to reload my navigations in my angular 5.2 application without any success. If the params are unchanged, the angular router will ignore my navigations.

How I navigate:

this.router.navigate(['/search', buildParamsFromSearchCriteria(criteria)]);

This navigates to:

/search;pageSize=20;page=1;orderBy=price;sortDirection=ASCENDING

My module config:

imports: [RouterModule.forRoot(appRoutes, { preloadingStrategy: PreloadAllModules, enableTracing: false, onSameUrlNavigation: 'reload' })],
like image 238
heldt Avatar asked Feb 23 '18 09:02

heldt


1 Answers

It can be done in a much simpler way. Below is a small sample code:

in routing.module: "/product/: id / details"

import { ActivatedRoute, Params, Router } from ‘@angular/router’;

export class ProductDetailsComponent implements OnInit {

    constructor(private route: ActivatedRoute, private router: Router) {
        this.route.params.subscribe(params => {
            this.paramsChange(params.id);
        });

    }

    // Call this method on page load
    ngOnInit() {
    }

    // Call this method on change of the param
    paramsChange(id) {
    }

the general explanation is ... why to destroy already existed component instance and create a new one for the same case when it means performance degradation? It is the exactly the same behavior as using routes pattern /product/:id where the same component instance is also kept for /product/5, /product/6, ...

So you should re-init the component on the base of some emitted event (resolver/guard) and not on the base of OnInit hook because of the same component instance.

like image 65
Pranay Mishra Avatar answered Nov 20 '22 11:11

Pranay Mishra