我正在尝试从以下URL获取URL参数
http://localhost:4200/#/candidate/login/?corporateid=66d8c83c-841a-42e8-945c-beb8c1af25ef&userid=cb2147ed-bd42-4b9f-82d4-0dcef861e24d&token=CfDJ8Ie7+UQ8GQRBtQNOMB6ytQXk0OvqW+/l9W1yh3gCLQw7Sa606eaQtWAC472uUrsUnMQtt69R4gF7f6aab/lIdToo9AfiwtJ7wlsdYnomPpo1OgOaIUZZVBtEaBxRfuJin/Rl+WO+AnvHAn6v4Sf2yO+2gkNotCw0yLsfgzKYOwOciVRDHWY5mNktbGsRCb96kbnxd6VyW02Vs7szuSBG4Ow5fFdtvVUkQDj+ne4HZQAyvts8pNOOkNT/g+cCbOO/wA==
我需要的参数值是用户ID,但我似乎得到的是空值。
这是我的代码
location = window.location;
urlHref = location.href;
urlConvert = new URL(this.urlHref);
if (location.toString().includes('userid')) {
this.userId = this.urlConvert.searchParams.get("userid");
console.log(this.userId);
} else {
this.userId = 'user';
console.log('gets user not candidate');
}
答案 0 :(得分:2)
您可以使用URLSearchParams
及其.get
函数来获取查询字符串值。
const str = 'http://localhost:4200/#/candidate/login/?corporateid=66d8c83c-841a-42e8-945c-beb8c1af25ef&userid=cb2147ed-bd42-4b9f-82d4-0dcef861e24d&token=CfDJ8Ie7+UQ8GQRBtQNOMB6ytQXk0OvqW+/l9W1yh3gCLQw7Sa606eaQtWAC472uUrsUnMQtt69R4gF7f6aab/lIdToo9AfiwtJ7wlsdYnomPpo1OgOaIUZZVBtEaBxRfuJin/Rl+WO+AnvHAn6v4Sf2yO+2gkNotCw0yLsfgzKYOwOciVRDHWY5mNktbGsRCb96kbnxd6VyW02Vs7szuSBG4Ow5fFdtvVUkQDj+ne4HZQAyvts8pNOOkNT/g+cCbOO/wA==';
const urlParams = new URLSearchParams(str);
const userId = urlParams.get('userid') || '';
console.log(userId);
答案 1 :(得分:1)
您还可以尝试使用目标组件:
private void dataFetch(Query query,int rvView){
FirestoreRecyclerOptions<Property> options = new FirestoreRecyclerOptions.Builder<Property>()
.setQuery(query, Property.class)
.build();
Toast.makeText(getContext(),options.toString(),Toast.LENGTH_LONG).show();
mAdapter = new DiscoverAdapter(options);
RecyclerView recyclerView = root.findViewById(rvView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
recyclerView.setAdapter(mAdapter);
mAdapter.startListening();
}
工作DEMO
答案 2 :(得分:1)
您可以使用ActivatedRoute
来获取路线参数。另外,可以通过使用queryParamMap
来获取始终新鲜的参数。因为queryParamMap
返回Observable<ParamMap>
:
constructor(
private route: ActivatedRoute
)
this.route.queryParamMap.subscribe(params => {
let userId = params.get('userId ');
});