我试图在我的Ionic 3应用程序中实现延迟加载。在我的应用程序中,我有一个配置提供程序,它从本地存储中获取/设置配置。
这是提供者的代码:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import 'rxjs/add/operator/map';
@Injectable()
export class ConfigProvider {
public name: string = null;
constructor(private storage: Storage) {}
initialize() {
return new Promise((resolve, reject) => {
Promise.all([
this.storage.get('name').then(value => this.name = value),
]).then(value => {
resolve();
});
});
}
setName(name: string): void
{
this.name = name;
this.storage.set('name', this.name);
}
}
我在MyApp组件的构造函数中调用initialize方法:
constructor(
public platform: Platform,
public menu: MenuController,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
private modalCtrl: ModalController,
private configProvider: configProvider
) {
configProvider.initialize().then(() => {
this.initializeApp();
});
}
但是,如果我直接从网址调用我的页面组件,我的提供程序的name属性为null ...
@IonicPage({
name: ‘test’,
segment: ’test’
})
@Component({
selector: 'page-test’,
templateUrl: 'test'
})
export class TestPage {
constructor(
private configProvider: configProvider) {
}
ngAfterViewInit() {
// this.configProvider.name is null !!!!
}
有人可以告诉我为什么吗? Thnax求助:)