主页在数据加载之前启动IONIC 2

时间:2022-10-01 22:59:44

The problem I have is really strange. In Ionic 2 in app.component.ts I have this code. The problem is that first it redirects me to Home Page and after that it loads data from the json. So as result at Home Page I get undefined.

我遇到的问题真的很奇怪。在app.component.ts的Ionic 2中,我有这个代码。问题是,首先它将我重定向到主页,然后它从json加载数据。因此,在主页的结果我得到了不确定。

The code in app.component.ts :

app.component.ts中的代码:

export class MyApp {

 public rootPage : any;

 constructor(public platform: Platform, data : Data) {

 this.platformready();
  data.loadData();

  this.rootPage = HomePage;

 }

 private platformready() {
   this.platform.ready().then(() => {

   Splashscreen.hide();

 });

 }

}

2 个解决方案

#1


0  

if your loadData return promise:

如果你的loadData返回承诺:

this.platformready();
  data.loadData().then(res => {
    // do something with data
    this.rootPage = HomePage;
  })
  .catch(err => {
    // alert error
  });
}

if return Observable

如果返回Observable

this.platformready();
  data.loadData().subscribe(res => {
    // do something with data
    this.rootPage = HomePage;
  },err => {
    // alert error
  });
}

#2


0  

Is Data a provider?

数据是提供者吗?

If it is just call it in your HomePage inside a ionViewWillLoad(){}, like this:

如果它只是在你的HomePage中在ionViewWillLoad(){}内调用它,就像这样:

constructor(data : Data) {}

ionViewWillLoad(){
  data.loadData();
}

And like this you can save it anywhere, in a variable, database, etc.

像这样你可以将它保存在任何地方,变量,数据库等。

If it's a data that changes later in the app, you can use ionViewWillEnter(){} instead.

如果它是稍后在应用中更改的数据,则可以使用ionViewWillEnter(){}。

Hope it helps :)

希望能帮助到你 :)

#1


0  

if your loadData return promise:

如果你的loadData返回承诺:

this.platformready();
  data.loadData().then(res => {
    // do something with data
    this.rootPage = HomePage;
  })
  .catch(err => {
    // alert error
  });
}

if return Observable

如果返回Observable

this.platformready();
  data.loadData().subscribe(res => {
    // do something with data
    this.rootPage = HomePage;
  },err => {
    // alert error
  });
}

#2


0  

Is Data a provider?

数据是提供者吗?

If it is just call it in your HomePage inside a ionViewWillLoad(){}, like this:

如果它只是在你的HomePage中在ionViewWillLoad(){}内调用它,就像这样:

constructor(data : Data) {}

ionViewWillLoad(){
  data.loadData();
}

And like this you can save it anywhere, in a variable, database, etc.

像这样你可以将它保存在任何地方,变量,数据库等。

If it's a data that changes later in the app, you can use ionViewWillEnter(){} instead.

如果它是稍后在应用中更改的数据,则可以使用ionViewWillEnter(){}。

Hope it helps :)

希望能帮助到你 :)