Ionic 4 ToastController按钮没有异步功能

时间:2019-09-12 14:14:53

标签: angular typescript ionic-framework ionic4

我需要在ToastController按钮的处理程序中实现一个函数,该函数返回一个Promise(在这里:this.navCtrl.navigateForward())。

这是我的程序代码:

const toast = await this.toastController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: () => {
        this.navCtrl.navigateForward('/destination');
      }
    }
  ]
});
await toast.present();

这给了我以下信息:Promises must be handled appropriately (no-floating-promises) 我想做的是这样:

const toast = await this.toastController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: async () => {
        await this.navCtrl.navigateForward('/destination');
      }
    }
  ]
});
await toast.present();

但这给我一个错误消息,该消息根本没有用:

Type '{ side: string; text: string; handler: () => Promise<void>; }' is not assignable to type 'string | ToastButton'.
  Type '{ side: string; text: string; handler: () => Promise<void>; }' is not assignable to type 'ToastButton'.
    Types of property 'side' are incompatible.
      Type 'string' is not assignable to type '"start" | "end"'.

我试图找到解决方案,但现在被卡住了。你有什么想法我错了吗?谢谢!

1 个答案:

答案 0 :(得分:1)

如果您需要等待页面转换,然后在那之后做一些事情,为什么不使用.then方法呢?

const toast = await this.toastController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: () => {
        this.navCtrl.navigateForward('/destination').then(()=>{
            // do something after page transition?
        })
      }
    }
  ]
});
await toast.present();