Angular2 Query Parameter from index URL

前端 未结 2 1902
梦毁少年i
梦毁少年i 2021-01-23 07:21

I am trying to get the query params from a url in a component in Angular2

Version: \"angular2\": \"npm:angular2@2.0.0-beta.12\",

I am trying to extract id query

相关标签:
2条回答
  • 2021-01-23 07:47

    You can get the query params from the _location.path() in the AppComponent. You'd have to parse them yourself. I'm looking for the same thing, and haven't found a better solution.

    I hope someone else follows up on this.

    0 讨论(0)
  • 2021-01-23 08:04

    RouteParams it's not available in the root component. It's only available to the routable components.

    See RouterOutlet soure code, in its activate function, you can see the following

    var providers = Injector.resolve([
      provide(RouteData, {useValue: nextInstruction.routeData}),
      provide(RouteParams, {useValue: new RouteParams(nextInstruction.params)}),
      provide(routerMod.Router, {useValue: childRouter})
    ]);
    

    Basically RouteParams is being injected through RouterOutlet to the routable components, you can't access it in non-routable components (sorry for repeating myself).

    0 讨论(0)
提交回复
热议问题