Angular2 Query Parameter from index URL

前端 未结 2 1933
梦毁少年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 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).

提交回复
热议问题