我已將 URL 中的引數傳遞給我的離子應用程式
http://localhost:8100/welcompage/overview?brand=bmw
我正在使用 ActivatedRoute 檢索作為 URL 中的引數傳遞的資料
constructor(private route: ActivatedRoute) {
}
ngOnInit() {
this.route.params.subscribe(params => {
console.log(params['brand']);
});
}
輸出總是“未定義”,同時 URL 中有引數
uj5u.com熱心網友回復:
第一種方法:
let brand = this.route.snapshot.paramMap.get('brand');
第二種方法:
this.route.paramMap.subscribe(
(data) => {
console.log(data.brand)
}
);
當您已經在路由 url/:brand 上定義了品牌引數時,方法 1 或方法 2 滿足您的需求。但是如果您使用查詢引數 url?brand=value1&property2=vaule2... 您可以使用方法 3 獲取查詢引數資料:
方法3:
this.route.queryParams
.subscribe(params => {
console.log(params); // { brand: "bmw" }
let brand = params.brand;
console.log(brand); // bmw
}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/476082.html
