Ionic framework 登录成功后如何调用app.component.ts

Ionic framework 登录成功后如何调用app.component.ts,ionic-framework,Ionic Framework,成功登录后,我将员工ID和用户名存储在本地存储中,并尝试访问app.component.ts,其中声明了配置文件菜单,因为app.component.ts初始化了我的应用程序的启动,所以无法获取在login.ts中设置的用户名 login.ts: if (login successful) { this.storage.set('empid', this.dbempid); this.storage.set('empname', this.dbusername); thi

成功登录后,我将员工ID和用户名存储在本地存储中,并尝试访问app.component.ts,其中声明了配置文件菜单,因为app.component.ts初始化了我的应用程序的启动,所以无法获取在login.ts中设置的用户名

login.ts:

if (login successful) {
    this.storage.set('empid', this.dbempid);
    this.storage.set('empname', this.dbusername);
    this.navCtrl.push(this.dashBoardPage); 
}
app.component.ts

this.storage.get('empname').then((val) => {
    this.connectedUsername = val;
});
app.html

{{connectedUsername}}

听起来您正在努力进行组件通信(登录页面需要告诉包含该组件的应用程序一些事情)。从我的角度来看,有两种选择:

  • 创建身份验证服务/提供程序。让Login.ts写入该服务中的属性。然后让app.component.ts和app.html显示来自此服务的数据(无论何时发生,都将由登录设置)
  • 使用事件。由于app.component.ts在用户登录(Login.ts)之前加载,因此一旦用户成功登录Login.ts页面,可能会触发事件。app.component.ts文件可以订阅该事件,并在此时检查存储中新保存的用户名。爱奥尼亚已经为您构建了一个友好的活动系统:

  • 您正在使用哪个存储?还有哪个版本的ionic?在app.component.ts中为get username编写代码?