0

Hi When I click on send message button code working fine with router but when I click there on goback link the code sends error on console and it stops.

You can check in stackbliz of my edit link as : https://stackblitz.com/edit/angular2-communicating-between-components-qjhrpu?file=app%2Ffeedback%2Ffeedback.component.html

Ganny
  • 63
  • 1
  • 1
  • 8

2 Answers2

1

You have to affect your subscription otherwise your this.subscription is undefined.

ngOnInit() {
  this.subscription = this.messageService.getNewUserInfo().subscribe(info => {
    this.message = info;
    console.log("here",info);
  })
}
Johan Rin
  • 1,900
  • 2
  • 20
  • 42
  • To manage you subscription, you should take a look at this post: [https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription](https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription) – Johan Rin Dec 28 '18 at 12:28
1

first you have to subscribe assign value to this.subscription.

ngOnInit() {
  this.subscription = this.messageService.getNewUserInfo().subscribe(info => {
    this.message = info;
    console.log("here",info);
  })
}

After that also check on it's value on ngOnDestroy.

ngOnDestroy() {
    // unsubscribe to ensure no memory leaks
    if(this.subscription) {
      this.subscription.unsubscribe();
    }
  }
Abhishek
  • 1,742
  • 2
  • 14
  • 25
  • Yes Abhishek it also need to check as after back message are taken in memory..Thanks you!! – Ganny Dec 28 '18 at 09:19