#Passing Params w/ Router

1 messages · Page 1 of 1 (latest)

placid nexus
#

Change [routerLink]="['/posts'] to [routerLink]="['/categories', category.id, 'posts'], and change the route path: 'posts' to path: 'categories/:categoryId/posts'.
Then in the PostsComponent, get the categoryId from the ActivatedRoute.paramMap observable in order to call the appropriate endpoint.

small kite
#

Any observable that you subscribe to, you should also call unsubscribe. Given the code you posted above, ngOnDestroy would be a good place for that

small kite
#

ngOnDestroy is only called when the component is removed from the page

#

Changing the params doesn't cause the component to destroy and re-init. It simply updates the params on the existing component. What you're seeing is exactly what I would expect

#

Why would you want to? It should be more efficient to re-use the component. At least, I imagine that's why Angular re-uses the component by default

#

I'm not sure what you're asking anymore. You're already subscribing to the route params. That subscribe block will execute whenever the params update

#

The only console.log I see in your code block is during ngOnInit. That method only gets called once, so the console.log will only fire once. The params will still go through the subscribe block

#

If you want to see it update in the console, move the console.log inside of the subscribe block