状态传递的Angular Ionic参数

问题描述 投票:0回答:2

如何在另一个页面的URL中接收state传递的参数? 我有这个代码将参数发送到下一页。

let costcountry = this.requestAmountData.costCenterCountryId    
this.router.navegateByUrl(`exampleurl`, {state: { costcountry: costcountry}});

但我不知道如何接收它以在下一页使用它。有任何想法吗? TK

angular typescript ionic-framework
2个回答
0
投票

尝试使用此方法

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 );

0
投票

第一种方法 - 路由器导航:

// 第一页:

构造函数(私有路由器:路由器){

}

函数名() {

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']; }

© www.soinside.com 2019 - 2024. All rights reserved.