ionic-framework - Ionic 4 : loading. 存在不是函数

标签 ionic-framework ionic4

Ionic 4,使用加载器时出错 loading.present 不是函数

这是我的代码:

const loading =  this.loadingController.create({
  message: 'Loading',
});

loading.present();

最佳答案

这是因为 loadingController.create() 是一个异步方法,在你在变量 loading 中获取 HTMLIonLoadingElement 的实例之前, 您正在调用目前未定义的 loading/present()

所以你需要等到在调用 loadingController.create()

的时候得到 HTMLIonLoadingElement 的实例

如何解决:简单使用 aync/await

const loading = await this.loadingController.create({
  message: 'Loading',
});
loading.present();

看到我们在 = 运算符之后使用了 await。所以它确保只有在调用 loadingController.create 完成并且变量加载被初始化时才执行下一行。

NOTE: Don't forget to add async keyword to the function inside which you are using the loader code, as we are using await.

关于ionic-framework - Ionic 4 : loading. 存在不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54053245/

相关文章:

ionic-framework - 如何修复 'Unsupported platform for fsevents@1.2.9: wanted {"操作系统“:"darwin","arch":"any"}(当前 : {"os" :"win32" ,"arch" :"x64"})

ionic-framework - ionic 服务不适用于 ionic2

android - ionic 应用程序在 Windows 上运行但不能在 mac 上使用 ios 模拟器运行

angular - 如何将数字分配给类型 'BehaviorSubject<number>'

ios - 如何解决 Ionic 4 到 IOS 应用程序中的错误 'EXC_CRASH (SIGKILL)'

ios - 如何允许 Cordova 6.1 应用程序从网站在 iOS 中嵌入 iframe?

ios - Ionic App 在应用商店上线 - Socket 现在不工作

php - 当我从硬编码的下拉列表中选择值时,它不会选择值并且不会将其发送到数据库中

android - 为 android 构建 ionic/react - 无效的项目类型 "react"

css - 打开 fab-list 时删除十字图标