Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从其他页面接收参数_Angular - Fatal编程技术网

Angular 如何从其他页面接收参数

Angular 如何从其他页面接收参数,angular,Angular,我正在做一个数字计算应用程序,我想把参数从一个页面传递到另一个页面,但我只得到第一个值。如何显示组件2中的其余值 我正在从page2中的数组中获取第一个值,但无法获取console.log中的其他值 组件1.ts:- for(var i=0;i

我正在做一个数字计算应用程序,我想把参数从一个页面传递到另一个页面,但我只得到第一个值。如何显示组件2中的其余值

我正在从page2中的数组中获取第一个值,但无法获取console.log中的其他值

组件1.ts:-

for(var i=0;i
组件2.ts:-

导出类操作组件实现OnInit{
f:[];
fa:[];
答复:[];
奎:[]
构造函数(专用路由:ActivatedRoute){}
恩戈尼尼特(){
this.f=parseInt(this.route.snapshot.paramMap.get(“firstNum”);
this.fa=parseInt(this.route.snapshot.paramMap.get(“secondNum”);
this.answer=parseInt(this.route.snapshot.paramMap.get(“answer”);
}
component2.html:-


等级
问题
{{f}}
+{{fa}}
卡片标题h5文本中心
{{答案}
回去
我希望输出为我的应用程序中显示的所有值

一个可能的解决方案

在component1.ts中:-尝试调用导航,如下所示:

this.router.navigate(['/operation',{firstNum:firstNum,secondNum:secondNum,answer:answer}]);

更新#1

firstNum
secondNum
answer
在for循环中声明并初始化,这是在调用
this.router.navigate()之前

您到底想传递给Component2什么


更新#2

仍然无法理解代码的用途,但请尝试以下操作

组件1.ts:-

var-firstNum;
var-secondNum;
var回答;
对于(var i=0;i
组件2.ts:-

导出类操作组件实现OnInit{
公共f:数字;
公共fa:编号;
公众回答:数字;
奎:[];//为什么?
构造函数(专用路由:ActivatedRoute){}
恩戈尼尼特(){
this.f=parseInt(this.route.snapshot.paramMap.get(“firstNum”);
this.fa=parseInt(this.route.snapshot.paramMap.get(“secondNum”);
this.answer=parseInt(this.route.snapshot.paramMap.get(“answer”);
}

您正在将
this.que.push({firstNum,secondNum});
firstNum和secondNum推到同一个数组中,对吗

那么,为什么不将整个数组发送到下一页,以便您可以按自己的方式访问它呢

当需要传递多个参数时,您当前所采用的方法将会并且可能会导致代码混乱和将来可能出现的锁定

所以


这种方法是有益的。

您可以通过两个步骤实现相同的效果:

第1步: 从一页导航到另一页时发送查询参数

this.router.navigate(['/operation'], {queryParams: {firstNum: firstNum, SecondNum: SecondNum, answer : answer }});
第二步: 在另一个组件中,您可以通过
ActivatedRoute
模块获取查询参数的值

this.firstNum= this._Activatedroute.snapshot.queryParams['firstNum']; 
this.SecondNum= this._Activatedroute.snapshot.queryParams['SecondNum']; 
this.answer = this._Activatedroute.snapshot.queryParams['answer'];
注意:要使用
ActivatedRoute
模块,必须将其插入组件中

 constructor( private _Activatedroute: ActivatedRoute)

你不连接页面,而是连接组件。Angular是一个单页应用程序。因此我不确定你所说的第二页是什么意思,你是指其他应用程序吗?如果答案不是,那么你需要设置一个路由。对不起,我不熟悉Angular。是的,这些是组件。你有几个选项……1.路由器->取决于组件的距离2.事件发射器事件3.通过服务可观察到。这里我使用navigate传递了数据,但第二页上只显示了第一个值所有数据都被传递到组件2(我可以在控制台上看到),但只显示了第一个值请查看更新#1和2
 constructor( private _Activatedroute: ActivatedRoute)