【问题标题】:Ionic 2: Event publish/subscribe not workingIonic 2:事件发布/订阅不起作用
【发布时间】:2017-07-10 19:10:15
【问题描述】:

用户登录,他们的登录信息被设置到本地存储并且他们被转发到 DashboardPage。 我要做的就是一旦用户登录并将用户转发到仪表板我想用登录的用户详细信息更新滑动菜单。

在仪表板页面的构造函数中,我已经发布了这样的事件 -

 let userinfo = this.storagehelper.getStorageItem("userinfo");
  this.event.publish("app:userLoggedIn", userinfo);

app.component.ts,我订阅了这样的事件-

 this.event.subscribe("app:userLoggedIn", userObject => {
       console.warn("Listening to published event");
       this.setMenuUsers(userObject);
  });

  setMenuUsers(userinfo){
      this.LoginUserEmail = userinfo.UserName;
      this.LoginUserName = userinfo.EmployeeName;
      this.LoginUserCompany = userinfo.CompanyName;
  }

问题

菜单没有更新,订阅事件根本不起作用。谁能帮助我做错了什么?

【问题讨论】:

  • 似乎您想从其他页面更新变量。在这个问题中查看我的answer
  • 可能是一个愚蠢的问题,但以防万一,app.component.ts 文件中的代码行 this.event.subscribe(...) 是否放置在您将用户重定向到 LoginPage 之前?
  • console.warn("Listening to published event"); 这是打印出来的吗?
  • @sebaferreras LoginPage 是登录页面,此页面没有滑动菜单。 Loginpage 重定向到 Dashboard 页面,该页面发布事件。但我猜 app.component.ts 是在登录页面呈现之前执行的。所以是的,订阅事件是首先执行的。
  • @suraj 没有打印任何内容,subscribe 不起作用

标签: ionic2


【解决方案1】:

试试这个方法- TS:

async saveUser () {
  let userinfo = await this.storagehelper.getStorageItem("userinfo");
  this.event.publish("app:userLoggedIn", userinfo);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 2018-12-18
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多