需要 Ionic platform.ready() 方法
Ionic platform.ready() method is needed
我有一个关于 platform.ready().then(() => {})
method.Do 的基本问题,我们每次使用本机插件时都需要使用这种方法吗?比如 Status bar
或 LocalStorage
等等?
如果我们仅在 app.component.ts
文件中使用上述方法,那么它是根组件,这还不够吗?在这个根组件希望 platform
为所有其他后续组件准备好之后,不是吗?那为什么我们也需要对每个其他子组件使用 ready
方法呢?因为我看过那么多文章和视频,如果有原生的话就用了plugin.Hope这个就不需要了吧?
在这个官方文档上你可以看到它在子组件内部使用了太多吗?你的意见? platform.ready().then(() => {})
platform.ready()
是一个承诺,一旦您的 device/native 插件准备就绪。
让我们看看 ionic sidemenu starter
模板 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 没有 "wait" 平台准备就绪。只有 statusBar.styleDefault()
和 splashScreen.hide()
调用等待该承诺。
假设要解决承诺需要很长时间,例如 5 秒。
如果您的 HomePage
中有任何离子本机代码,您在 app.component.ts
或任何其他页面中使用的任何提供程序(因为用户可能已经在该应用程序中导航) , ionic-native 调用将会失败,因为平台还没有准备好。
举个例子:
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()
以确保平台可用。在实践中,这真的取决于具体情况,因为通常平台准备得非常快,如果你不使用它,你不会注意到任何差异。但如果你想确定,你应该在任何地方都使用它。
我有一个关于 platform.ready().then(() => {})
method.Do 的基本问题,我们每次使用本机插件时都需要使用这种方法吗?比如 Status bar
或 LocalStorage
等等?
如果我们仅在 app.component.ts
文件中使用上述方法,那么它是根组件,这还不够吗?在这个根组件希望 platform
为所有其他后续组件准备好之后,不是吗?那为什么我们也需要对每个其他子组件使用 ready
方法呢?因为我看过那么多文章和视频,如果有原生的话就用了plugin.Hope这个就不需要了吧?
在这个官方文档上你可以看到它在子组件内部使用了太多吗?你的意见? platform.ready().then(() => {})
platform.ready()
是一个承诺,一旦您的 device/native 插件准备就绪。
让我们看看 ionic sidemenu starter
模板 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 没有 "wait" 平台准备就绪。只有 statusBar.styleDefault()
和 splashScreen.hide()
调用等待该承诺。
假设要解决承诺需要很长时间,例如 5 秒。
如果您的 HomePage
中有任何离子本机代码,您在 app.component.ts
或任何其他页面中使用的任何提供程序(因为用户可能已经在该应用程序中导航) , ionic-native 调用将会失败,因为平台还没有准备好。
举个例子:
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()
以确保平台可用。在实践中,这真的取决于具体情况,因为通常平台准备得非常快,如果你不使用它,你不会注意到任何差异。但如果你想确定,你应该在任何地方都使用它。