Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 需要Ionic platform.ready()方法_Angular_Typescript_Ionic Framework_Ionic2_Ionic3 - Fatal编程技术网

Angular 需要Ionic platform.ready()方法

Angular 需要Ionic platform.ready()方法,angular,typescript,ionic-framework,ionic2,ionic3,Angular,Typescript,Ionic Framework,Ionic2,Ionic3,我有一个关于platform.ready()。然后(()=>{})方法的基本问题。我们每次使用本机插件时都需要使用这个方法吗?像状态栏或本地存储等 如果我们只在app.component.ts文件中使用上述方法,那么它就是根组件,这还不够吗?在此根组件之后,希望平台为所有其他后续组件做好准备否?那么,为什么我们还需要使用ready方法以及其他每个子组件呢?因为我看过很多文章和视频,如果有本地插件的话,我会用到它们。希望这不是必需的 在这个官方文档中,你可以看到它在子组件中使用的太多了没有?你的想

我有一个关于
platform.ready()。然后(()=>{})
方法的基本问题。我们每次使用本机插件时都需要使用这个方法吗?像
状态栏
本地存储

如果我们只在
app.component.ts
文件中使用上述方法,那么它就是根组件,这还不够吗?在此根组件之后,希望
平台
为所有其他后续组件做好准备否?那么,为什么我们还需要使用
ready
方法以及其他每个子组件呢?因为我看过很多文章和视频,如果有本地插件的话,我会用到它们。希望这不是必需的

在这个官方文档中,你可以看到它在子组件中使用的太多了没有?你的想法

platform.ready()
是一个承诺,一旦您的设备/本机插件准备就绪,它就会得到解决

让我们看看
离子侧菜单启动程序
模板

正如您在第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中的每一个承诺一样,您无法判断它何时解决。正如你在代码中看到的,爱奥尼亚应用程序不会“等待”平台准备就绪。只有
statusBar.styleDefault()和
splashScreen.hide()调用等待该承诺

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

如果您的
主页
、您在
app.component.ts
或任何其他页面中使用的任何ionic本机代码(因为用户在此期间已经可以在应用程序中导航),ionic本机调用将失败,因为平台尚未准备就绪

例如:

 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()?没有。你有关于只在根组件(即
app.component.ts
)中使用它的信息吗@ChristianBenselerHope我为你找到了一些东西。请看这个。它甚至在子组件中也使用。你的想法@ChristianBenseler如果我在
root
组件中使用
async/await
模式和
promise
,该怎么办?如果使用
async/await
实现该承诺,那么接下来的所有ionic本机代码都可以。但是:
rootPage=HomePage
仍然发生在
async/await
函数之外,因此那里的一切仍然可能崩溃,因为
async/await
不会停止整个javascript代码的执行。要解决这个问题,您可以在
platform.ready()
内部/之后分配根页面。但是这会阻止你的整个应用程序,我不推荐。摘要在
app.component.ts
中,代码不会“等待”平台准备就绪