Angular router: (TAB like) navigate - how to change URL without running ngOnInit again

11,841

I finally found the solution: Change route params without reloading in angular 2

it is possible just to use location.replaceState, works like a charm in my case!

Share:
11,841
DS_web_developer
Author by

DS_web_developer

Updated on June 26, 2022

Comments

  • DS_web_developer
    DS_web_developer almost 2 years

    OK, the question is simple, but don't seem to find the answer myself, so please help

    I have this routes (simplified):

    const routes: Routes = [
      {path: '', component: ListPageComponent},
      {path: 'd/:id', component: DetailsPageComponent},
      {path: ':param1', component: ListPageComponent},
      {path: ':param1/:param2', component: ListPageComponent},
      {path: ':param1/:param2/:param3', component: ListPageComponent},
      {path: '**', component: ListPageComponent}
    ];
    

    On the ListPageComponent I have some select box filters on top, that trigger onFilter method (simplified):

    onFilter(filters: any) {
      this._router.navigate([
        'myroute/',
        this.param1,
        this.param2,
        this.param3
      ]);
    }
    

    Now, what I would like is when I trigger this method, and I navigate (to the same page component), I don't want the component to run ngOnInit again... just to change the URL params (I am already subscribed to params change - this._route.params.subscribe)