警报控制器设置消息在离子 4 中不起作用

alert controller setmessage not working in ionic 4

在 alertController 中,如果输入字段为空,我想调用 prompt.setMessage() 。但这在 ionic4 中无效

代码

 const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: async (data:any) => {
            if(data.itemtext==""){
              prompt.setMessage("text should not be empty");
              return false;
            }
            else{
            console.log("data.itemtext");
            }
          }
        }
      ]
    });
    await prompt.present();

如果文本为空,我不想关闭警报提示 请帮忙

你不再有Ionic 4中的方法,但你仍然可以直接更改消息属性来实现你想要的:

const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: (data:any) => {
            if(data.itemtext==""){
              prompt.message = "text should not be empty";
              return false;
            }
            else{
              console.log(data.itemtext);
            }
          }
        }
      ]
    });
    await prompt.present();

我还从处理程序中的方法中删除了 "async",因为那里不需要它。