0

The user logs in, their login info is set to local storage and they are forwarded to DashboardPage. All I want to do is once user is logged in and user is forwarded to Dashboard I want to update Swipe menu with Logged in user details.

In constructor of Dashboard page, I have published the event like this -

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

and in app.component.ts, I subscribe to the event like this -

 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;
  }

PROBLEM

The menu is not getting updated, subscribe event is not working at all. Can anybody help what I am doing wrong?

WatsMyName
  • 4,240
  • 5
  • 42
  • 73
  • Seem like you want to update varible from other page. See my [answer](https://stackoverflow.com/questions/44715181/how-to-update-a-vaiable-in-component-ts-file-from-another-ts-file/44753236#44753236) in this question – Duannx Jul 10 '17 at 06:32
  • May be a silly question, but just in case, is the line of code `this.event.subscribe(...)` from the `app.component.ts` file, placed before you redirect the user to the LoginPage? – sebaferreras Jul 10 '17 at 06:54
  • `console.warn("Listening to published event");` is this getting printed? – Suraj Rao Jul 10 '17 at 06:56
  • @sebaferreras LoginPage is the landing page and this page doesn't have Swipe menu. Loginpage redirects to Dashboard page, which publishes the event. But I guess app.component.ts is executed right before Login page is rendered. so yes subscribe event is what executes first. – WatsMyName Jul 10 '17 at 07:08
  • @suraj no nothing is getting printed, `subscribe ` isn't working – WatsMyName Jul 10 '17 at 07:08
  • How did you import your events object? Is it declared correctly? Did you import using `events` with an `s`? I am also using events and your code looks fine – Huiting Jul 10 '17 at 07:25

1 Answers1

1

Try this way- TS:

async saveUser () {
  let userinfo = await this.storagehelper.getStorageItem("userinfo");
  this.event.publish("app:userLoggedIn", userinfo);
}
Swapnil Patwa
  • 4,069
  • 3
  • 25
  • 37