9

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)

4
  • 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. Commented Dec 18, 2017 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. Commented Dec 18, 2017 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. Commented Dec 18, 2017 at 20:23
  • well, they have to be different routes like /myroute/param1/param2/... Commented Dec 19, 2017 at 6:45

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.