首页 > 解决方案 > Ionic 3如何接收从一页到另一页的数组

问题描述

我需要将数组从一个页面传递到另一个页面。在第一页中,数组是通过从列表中选择项目来创建的,它会推送所有项目,但是当我在另一个页面中收到这个数组时,它变成了一个数组对象,我无法读取我的 html 页面中的数据,这里是我的代码的一部分:

labo.ts - 这是我创建数组的方式,它很好

selectItem(itemKey){
   const foundAt = this.selectedQuestions.indexOf(itemKey);
   if (foundAt >= 0) {
      this.selectedQuestions.splice(foundAt, 1);
   } else {
      this.selectedQuestions.push(itemKey);
   }
  console.log(this.selectedQuestions);

}

添加完项目后,我将其发送到 calendar.ts

calendar(){
 console.log("ARRAY ", this.selectedQuestions)
    this.navCtrl.push(CalendarioPage,{
      queryParams: {
           value : this.selectedQuestions
       },
  });

}

这是 calendaar.ts,我需要在其中接收数组:

 export class CalendarioPage {
  datos: Array<object>;

  constructor(public navCtrl: NavController, public navParams: NavParams,
    public http: Http, private conteSearch : ContesearchProvider) {

    this.datos = navParams.get("queryParams");

    console.log("array -> ", this.datos)

  }  
 }

在我的 calendar.html 文件中,我尝试读取数组:

<div *ngFor="let dato of datos">
  {{dato.nombre}} 
</div>

我收到了这个错误“错误错误:”未捕获(承诺):错误:找不到“对象”类型的不同支持对象“[对象对象]”。NgFor 仅支持绑定到 Iterables,例如 Arrays。”

这就是它在数组之前和之后的样子(当在 calendar.ts 中收到时)

在此处输入图像描述

提前感谢您的任何想法!

标签: typescriptionic3angular4-router

解决方案


您传递的数组在对象 this.datos 的值键处可用

因此,您需要像 this.datos.value 一样阅读它,这应该可以。

谢谢


推荐阅读