加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 离子platform.ready()方法是必需的

发布时间:2020-12-17 17:28:51 所属栏目:安全 来源:网络整理
导读:我有一个关于platform.ready().then(()= {})method的基本问题.我们每次使用本机插件时都需要使用此方法吗?像状态栏或LocalStorage等? 如果我们仅在app.component.ts文件中使用上述方法,这是不够的,因此它是根组件?在这个根组件希望平台准备好所有其他后续
我有一个关于platform.ready().then(()=> {})method的基本问题.我们每次使用本机插件时都需要使用此方法吗?像状态栏或LocalStorage等?

如果我们仅在app.component.ts文件中使用上述方法,这是不够的,因此它是根组件?在这个根组件希望平台准备好所有其他后续组件之后没有?那么为什么我们还需要使用ready方法来处理每个其他子组件呢?因为如果有任何原生插件,我已经看过很多文章和视频.希望这不是不需要的吗?

在这个官方文档中你可以看到它在子组件内部使用了吗?你的意见? platform.ready().then(() => {})

解决方法

platform.ready()是一个在您的设备/本机插件准备就绪后解析的承诺.

我们来看看离子侧面菜单启动器模板https://github.com/ionic-team/ionic2-starter-sidemenu/blob/master/src/app/app.component.ts.

正如您在第15行的app.component.ts中所看到的,rootPage已设置并将尽快加载.在构造函数this.initializeApp();电话

this.platform.ready().then(() => {
  // Okay,so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  this.statusBar.styleDefault();
  this.splashScreen.hide();
});

与javascript中的每个承诺一样,您无法分辨它何时结算.正如您在代码中看到的那样,ionic-app并没有“等待”平台准备就绪.只有statusBar.styleDefault()和splashScreen.hide()调用才会等待该承诺.

假设需要很长时间来解决承诺,例如5秒.

如果您的主页中有任何离子本机代码,您在app.component.ts或任何其他页面中使用的任何提供程序(因为用户可能已经在该时间内浏览应用程序),离线本机调用将失败,因为平台尚未准备好.

举个例子:

constructor(public platform: Platform,public statusBar: StatusBar,public splashScreen: SplashScreen,private: qrScanner: QrScanner) {
      this.initializeApp();

      this.qrScanner.scan(); // Let's assume this is a provider we made to start a QR scanner. It will try to load the scanner immediately,regardless of the state of the platform.ready() promise. So if the platform is not ready,it will crash.

      // used for an example of ngFor and navigation
      this.pages = [
        { title: 'Home',component: HomePage },{ title: 'List',component: ListPage }
      ];

  }

这意味着理论上,在使用本机插件确保平台可用时,应始终使用this.platform.ready().在实践中,它实际上取决于具体情况,因为通常平台准备得非常快,如果您不使用它,您将不会注意到任何差异.但如果你想确定,你应该到处使用它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读