Events Pub/Sub How to unsubscribe in lifecycle hook

This doesn’t make sense to me. What code are you trying? I was thinking of something like this:

private _loginsub: (user:User) => void;

ngOnInit():void {
  this._loginsub = (user) => {
    this.loginHandler(user);
  };
  this.events.subscribe('user:loggedin', this._loginsub);
}

onPageWillLeave():void {
  if (this._loginsub) {
    this.events.unsubscribe('user:loggedin', this._loginsub);
    this._loginsub = undefined;
  }
}

EDIT: fixed a couple of syntax errors caught by @inki.

16 Likes