0

I have a container Component AdminComponent which has different childs:

{ path: ":company/admin", component: AdminComponent, children: [ { path: "", component: LoginComponent }, { path: "account", component: AccountComponent, canActivate: [RedirectGuard] }, { path: "detail", component: AdminlayoutsComponent, canActivate: [RedirectGuard] } ] } 

But my route subscription is only fired once and not on route change:

export class AdminComponent { constructor( private activatedRoute: ActivatedRoute ) { this.activatedRoute.params.subscribe((params: Params) => { console.log("####params", params); }); } } 

How can I trigger the route change event on every route change in my parent component?

5
  • Have you tried with RouterModule.forRoot(routes, {onSameUrlNavigation: 'reload'}) in your main routing module? Commented Oct 4, 2019 at 9:38
  • tried this now without succes @Robertgarcia Commented Oct 4, 2019 at 9:40
  • stackblitz.com/edit/onsameurlnavigation-demo please look on it Commented Oct 4, 2019 at 9:40
  • Seems weird, i have the same structure in a project and it works, can you provide a repo? Commented Oct 4, 2019 at 9:48
  • I think I have no param change (only route change). Commented Oct 4, 2019 at 12:05

2 Answers 2

1

In Angular, every route is its own instance.

This means that if you want to listen to the params of the child, you have to listen to the child of the route you're on.

Here is a demo showing it.

 constructor(private route: ActivatedRoute) { route.children[0].params.subscribe(params => { console.log(params); }); } 

Although I should warn you, this isn't a very good practice, because the parent not always know if the child is there (while the opposite isn't true). You should just listen to routing events in the child component.

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

Comments

0

events are not fired because: in path: ":company/admin", only first part of path is the param. Right event listener is:

 router.events.subscribe((val) => { // do stuff )}; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.