无法在两页之间传递数组

时间:2019-08-06 10:38:48

标签: angular ionic-framework ionic3

是离子的新手,目前陷入此问题,我正试图传递具有这种结构的对象

{
car: {...}
reservations:[{...},{...},...]
}

我做了这段代码,但是a只能发送car对象,但是不能发送预订数组。

第1页(发送者):

  //class success object

  class SuccessObject {
    success: any;

    public constructor(success: any) {
        this.success = success;
    }
  }
  public getCar(id){
    let loader = this.loading.get('Chargement de la reservation...');
    loader.present();
    this.data.getCar(id).subscribe(
        data => {
          //DATA is full with car infos and its reservations
          let infos: SuccessObject = new SuccessObject(data);
            this.navCtrl.push(CarinfosPage, {
                'car': infos.success.car,
                'res': infos.success.reservation,
            });
            loader.dismiss();
        },
        err => {
            loader.dismiss();
        }
    );  
  }

第2页(接收者):

export class CarinfosPage {

  public data;
  reservations = <any>{};
  car = <any>{};

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.reservations = this.navParams.data;
    this.car = this.navParams.get('car');
  }

  ionViewDidLoad() {
    console.log(this.car); // i get car infos here
    console.log(this.reservations); // i get undefined here
  }

}

2 个答案:

答案 0 :(得分:3)

您没有得到this.navParams.get('res');

    this.reservations = this.navParams.get('res');


 ionViewDidLoad() {
    console.log(this.car); // i get car infos here
    console.log(this.reservations); // i get undefined here
  }

答案 1 :(得分:1)

替代将大型数组有效负载通过路由器传递到第二页的方法是,仅发送id;然后有一个数据服务,第二页将使用该数据服务来检索数据并获取其所需的所有信息。

第1页(发送者)

  public getCar(id){
     this.navCtrl.push(CarinfosPage, { id: id });
  }

第2页(接收方):

export class CarinfosPage implements OnInit {
  public data;
  reservations = <any>{};
  car = <any>{};

  // TODO inject the data service
  constructor(
      public navCtrl: NavController,
      public navParams: NavParams,
      public dataService: DataService
  ) {}

 ngOnInit() {
    const id = this.navParams.get('id');

    this.dataService.getCar(id).subscribe(
        data => {
         // TODO Get the car and reservations information here
          ...
        },
        err => {
           ...
        }
    );  
 }
}