#How To Get Route Path Parameters In Non-Routed Angular Components?

9 messages · Page 1 of 1 (latest)

kindred nova
#

I need to get route parameter in nav bar, is it possible?

<nav-bar></nav-bar>
<router-outlet></router-outlet>
green furnace
#

Not easily. Why not put nav-bar in the route?

kindred nova
green furnace
#
const routes: Routes = [
  {
    path: '',
    component: MainLayoutComponent,
    children: [
      // your routes here
    ]
  }
]

App component:

<router-outlet /> <!-- <- main routes (in this example just MainLayoutComponent) render here -->

Main layout:

<nav-bar></nav-bar>
<router-outlet /> <!-- <- children of main route render here -->
next briar
#

But that still won't allow the navbar to access the child route's parameters. @kindred nova what's your usecase. If the navbar is global for all routes, and not part of route X, why does it need to access the parameters of route X?

kindred nova
next briar
# kindred nova I have like search bar in navbar and i just need to get route parameter on init

OK, but the actual search will probably be done by the routed component, and the results will be displayed by the routed component as well, won't it? So the search bar needs to somehow communicate with the routed component somehow (via a service probably). So the routed component could tell the navbar, via the same service, what the current search query is.
I'm making a lot of guesses here. That's why I asked for your use-case. Your very short answer doesn't help much.

kindred nova
#

Okey, then I will create service and communicate them via this service