当用户登录时,你可以公共事件(离子事件),在您的组件,只需订阅你定义的事件。每当事件发生时,您都可以更新您的财产。
您的应用程序无法正常工作,因为在早期状态下,存储不会保留任何数据,即使您从存储获取数据,也没有任何数据。
constructor(
private events: Events,
private storage: Storage,
//...
) { }
login(/* params */) {
// after login successful, save name to storage
this.storage.set('name', JSON.stringify(name))
.subscribe(() => {
this.events.publish('login:success');
});
}
上的分量:
name: string;
constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, private events: Events) {
this.storage.get('name').then((val) => {this.name= val;});
this.event.subscribe('login:success',() => {
this.storage.get('name').then((val) => {this.name= val;});
})
}
}
什么时候存储在'this.storage'中?你可以添加那部分? –
在上一个登录页面。我的意思是一切正常,除了第一次加载网站。 – user33276346