如何在另一个页面的URL中接收state传递的参数? 我有这个代码将参数发送到下一页。
let costcountry = this.requestAmountData.costCenterCountryId
this.router.navegateByUrl(`exampleurl`, {state: { costcountry: costcountry}});
但我不知道如何接收它以在下一页使用它。有任何想法吗? TK
尝试使用此方法
ActivatedRoute
:
第 1 页:
gotoYourPage(paramsToSend){
this.router.navigate(['/YourPageName', { yourDataKey : JSON.stringify(paramsToSend)}]);
}
第2页:
import { ActivatedRoute } from '@angular/router';
constructor(
private aroute: ActivatedRoute
) { }
this.receiveData = JSON.parse(this.aroute.snapshot.params.yourDataKey );
第一种方法 - 路由器导航:
// 第一页:
构造函数(私有路由器:路由器){
}
函数名() {
this.router.navigateByUrl('/secondpage', { state: { id: id_data, name: txt_名称} }) }
// 第二页:
列表数据:任意;
id:任意;
名称:任意;
构造函数(私有路由器:路由器){
this.listdata = this.router.getCurrentNavigation()?.extras.state;
this.id = this.listdata['id'];
this.name = this.listdata['name'];
}
第二种方法 - HTML RouterLink:
首页:
第二页:
列表数据:任意;
id:任意;
名称:任意;
构造函数(私有路由器:路由器){
this.listdata = this.router.getCurrentNavigation()?.extras.state;
this.id = this.listdata['id'];
this.name = this.listdata['name']; }