警报控制器 setmessage 在 ionic 4 中不起作用

Bha*_*hri 2 angular ionic4

在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();
Run Code Online (Sandbox Code Playgroud)

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

Ser*_*nko 5

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

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();
Run Code Online (Sandbox Code Playgroud)

我还从处理程序中的方法中删除了“异步”,因为您在那里不需要它。