简体   繁体   English

需要Ionic platform.ready()方法

[英]Ionic platform.ready() method is needed

I have a basic question about platform.ready().then(() => {}) method.Do we need to use this method each and every time when we use a native plugin? 我有一个关于platform.ready().then(() => {}) method的基本问题platform.ready().then(() => {})我们每次使用本机插件时都需要使用此方法吗? Like Status bar or LocalStorage or etc? Status barLocalStorage等?

Is that not enough if we use above method only inside the app.component.ts file hence it is the root component? 如果我们仅在app.component.ts文件中使用上述方法,这是不够的,因此它是根组件? After this root component hope platform is ready for all the other subsequent components no? 在这个根组件希望platform准备好所有其他后续组件之后没有? Then why do we need to use ready method each and every other child components too? 那么为什么我们还需要使用ready方法来处理每个其他子组件呢? Because I have seen so many articles and videos where it uses if there is any native plugin.Hope this is not needed no? 因为如果有任何原生插件,我已经看过很多文章和视频。希望这不是不需要的吗?

On this official doc where you can see that it uses inside the child component too no? 在这个官方文档中你可以看到它在子组件内部使用了吗? Your thoughts? 你的意见? platform.ready().then(() => {}) platform.ready()。then(()=> {})

platform.ready() is a promise that resolves once your device/native plugins are ready. platform.ready()是一个在您的设备/本机插件准备就绪后解析的承诺。

Let's look at the ionic sidemenu starter template https://github.com/ionic-team/ionic2-starter-sidemenu/blob/master/src/app/app.component.ts . 我们来看看ionic sidemenu starter模板https://github.com/ionic-team/ionic2-starter-sidemenu/blob/master/src/app/app.component.ts

As you can see in the app.component.ts on line 15 the rootPage gets set and will get loaded as soon as possible. 正如您在第15行的app.component.ts中所看到的, app.component.ts已设置并将尽快加载。 In the constructor this.initializeApp(); 在构造函数this.initializeApp(); calls 电话

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();
});

As with every promise in javascript, you can't tell when it resolves. 与javascript中的每个承诺一样,您无法分辨它何时结算。 And as you can see in the code, the ionic-app does not "wait" for the platform to be ready. 正如您在代码中看到的那样,ionic-app并没有“等待”平台准备就绪。 Only the statusBar.styleDefault() and splashScreen.hide() calls wait for that promise. 只有statusBar.styleDefault()splashScreen.hide()调用splashScreen.hide()等待该承诺。

Let's say it takes a very long time for the promise to be resolved, for example 5 seconds. 假设需要很长时间来解决承诺,例如5秒。

If you have any ionic-native code in your HomePage , any providers that you use inside app.component.ts or any other page (because the user could already navigate around the app during that time), the ionic-native call will fail because the platform is not ready yet. 如果您的HomePage有任何离子本机代码,您在app.component.ts或任何其他页面中使用的任何提供程序(因为用户可能已经在该时间内浏览应用程序), app.component.ts本机调用将失败,因为平台尚未准备好。

As an example: 举个例子:

 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 means that in theory, you should always use this.platform.ready() when using native plugins to make sure the platform is available. 这意味着理论上,在使用本机插件确保平台可用时,应始终使用this.platform.ready() In practice it really depends on the specific case because often the platform is ready very fast and you won't notice any difference if you don't use it. 在实践中,它实际上取决于具体情况,因为通常平台准备得非常快,如果您不使用它,您将不会注意到任何差异。 But if you want to be sure, you should use it everywhere. 但如果你想确定,你应该到处使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM