Angular Get queryParam from URL when no name is specified

怎甘沉沦 提交于 2019-12-12 06:51:34

问题


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.



来源:https://stackoverflow.com/questions/43056847/angular-get-queryparam-from-url-when-no-name-is-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!