首页 > 解决方案 > 从操作表按钮打开模态视图不起作用

问题描述

我正在做一个学校项目,我想从操作表按钮打开一个模式视图,但我确实在下面收到此错误, TypeError: Cannot read property 'addMedicationModalCtrl' of undefined但是当我在操作表之外使用离子按钮时,它可以工作。

下面示例代码的快照

constructor(
    private addMedicationModalCtrl: ModalController,
    private actionSheetCtrl: ActionSheetController,

  ) { }

  ngOnInit() {

  }

  addMedication() {
    console.log("add medication");
    this.addMedicationModalCtrl.create({
      component: CreateMedicationComponent
    }).then(modalEle => {
      modalEle.present();
    });
  }


  onTreatment() {
    const actionSheet = this.actionSheetCtrl.create({
      header: 'Treatment',
      buttons: [
        {
          text: 'Medication',
          role: 'medication',
          handler: this.addMedication,
        },
      ],

    })
    actionSheet.then(actionSheetEle => {
      actionSheetEle.present();
    });
  }

标签: angulartypescriptionic-framework

解决方案


请稍微更改您的操作表处理程序函数。它会开始为你工作

onTreatment() {
    const actionSheet = this.actionSheetCtrl.create({
        header: 'Treatment',
        buttons: [
            {
                text: 'Medication',
                role: 'medication',
                handler: () => {
                    this.addMedication()
                },
            },
        ],

    })
    actionSheet.then(actionSheetEle => {
        actionSheetEle.present();
    });
}

推荐阅读