离子4 - 如何检索传递给模态的数据

Han*_*Che 3 ionic-framework angular ionic4

根据Ionic 4文档,您可以通过componentProps属性传递数据.在Ionic3中,我可以使用navParams服务检索它.我如何在Ionic 4中做到这一点?

async presentModal() {
    const modal = await this.modalController.create({
      component: ModalPage,
      componentProps: { value: 123 }
    });
    return await modal.present();
  }
Run Code Online (Sandbox Code Playgroud)

Sur*_*iya 15

你需要使用@Input()装饰器.

async presentModal() {
    const modal = await this.modalController.create({
      component: ModalPage,
      componentProps: { value: 123 }
    });
    return await modal.present();
  }
Run Code Online (Sandbox Code Playgroud)

零件:

@Component({
  selector: 'ModalPage',
  templateUrl: './ModalPage.component.html',
  styleUrls: [ './ModalPage.component.css' ]
})
export class ModalPage  {
  name = 'Angular';
  @Input() value: any;
}
Run Code Online (Sandbox Code Playgroud)

  • 注意,输入值在组件的构造函数中尚不可用;要尽早访问它,您将需要定义ngAfterViewInit()方法。 (2认同)