-->

Angular Get queryParam from URL when no name is sp

2019-08-26 08:47发布

问题:

How do you get query information from a URL like this:

http://localhost:4200/reset?c564e265451e2e24be460f30271678b566d20fa9962000063bb6d079f5e376a0=

If it was http://...reset?token=c56... I can grab the value using queryParams by doing something like:

this.route.queryParams.subscribe((params)=>this.token = params['token']);`

but I can't figure out how to get the token when the query parameter has no name. I can see the value under _value when logging this.route.queryParams to the console.

回答1:

I figured out a couple solutions:

The first solution I found seemed a bit ugly:

this.route.queryParams.forEach((value) => this.token = Object.keys(value).shift());

Then I found queryParamMap, which made it less ugly:

this.route.queryParamMap.subscribe((value) => this.token = value.keys.shift());

So I've used the queryParamMap solution unless someone can find a short hand way of doing the same thing.