【问题标题】:Opening a Modal view from action-sheet button is not working从操作表按钮打开模态视图不起作用
【发布时间】:2021-01-15 13:58:03
【问题描述】:

我正在做一个学校项目,我想从操作表按钮打开一个模式视图,但我确实在下面收到此错误 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();
    });
  }

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

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

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

    【讨论】:

    • 是的,这很神奇,感谢您的帮助。但是我为什么一开始没有工作,我的代码确实调用了该方法,因为我尝试使用 console.log 来测试该方法是否被实际调用,但它确实有效。 @TaylorRahul
    • 它是因为胖箭头函数和普通的javascript函数..胖箭头函数可以访问最近的非箭头父函数的参数对象。而非胖箭头功能只能访问其功能...更多详情请查看以下网址medium.com/better-programming/…
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多