我试图获取从安全API返回的网址查询字符串,因此我的网址如下www.mysite.com/profile#code=xxxx&id_token=xxx
我希望获得code and id_token
的值。
我尝试在ngOnInit()
中使用activatedRoute,但params为undefined
。如果这是正确的方法,有人可以解雇吗?我创建了一个函数,但它不是获取URL查询字符串的角度方式。
this.routeParam.queryParams
.subscribe(params => {
let code = params['id_token'];
console.log('code val >> ', code);
})
我的尝试有效,但我想以有角度的方式去做。
getURLVars(key) {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars[key];
}
答案 0 :(得分:0)
尝试一下:
使用快照,您可以从URL获取查询字符串。
ngOnInit() {
let code = this.router.snapshot.queryParams['id_token'];
}