How to access Angular2 new router querystring

2019-02-20 07:13发布

问题:

How can I access the querystring parameters from my component? I have URLs like this:

http://myurl.com/myroute?myparam=1

thanks

回答1:

Hint The RC.1 @angular/router is also deprecated. It's better to stick with @angular/router-deprecated (because it has more features and is quite stable) until a new new router is shipped.


If you are using router-deprecated then constructor(private params: RouteParams) will give you access to all parameters, then you can get specific parameter by params.get('myparam')

working of Router in RC is not yet documented, but you can get hold of params in routerOnActivate

routerOnActivate(curr: RouteSegment, prev?: RouteSegment, 
             currTree?: RouteTree, prevTree?: RouteTree): void {
  let myparam = curr.getParam("myparam");
}