8

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)

DS_web_developer
  • 3,622
  • 13
  • 52
  • 83
  • Did you try navigating relatively to the `ActivatedRoute`? I think that shouldn't reload your component but I'm not that sure, since I've only done it with `queryParams`. – Osman Cea Dec 18 '17 at 20:13
  • Btw, looking at your routes and since you're using some kind of form element, I think you should be better off with `queryParams` instead. – Osman Cea Dec 18 '17 at 20:19
  • @OsmanCea is right. With router params you will have different component for each route - so you can have the same component only if previous route has the same shape. But with queryParams you can describe everything with one route = one component initialization. – Pavel Agarkov Dec 18 '17 at 20:23
  • well, they have to be different routes like /myroute/param1/param2/... – DS_web_developer Dec 19 '17 at 06:45

1 Answers1

3

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!

DS_web_developer
  • 3,622
  • 13
  • 52
  • 83