Unsubscribe used to remove the subscription from from subscribed observable. this is usually implemented on OnDestry() method. Its always best practice to unsubscribe once the use of subscription is no more used.

Here is the sample code for implementation of unsubscribe()


import { Component, OnInit, OnDestroy } from '@angular/core';
import {PreviewService} from '../services/preview-service.service';
import { Subscription } from '../../../node_modules/rxjs';

@Component({
  selector: 'second-component',
  templateUrl: './second-component.component.html',
  styleUrls: ['./second-component.component.css']
})
export class SecondComponentComponent implements OnInit,OnDestroy {

  public previewText:string;
  public sub:Subscription;

  constructor(private previewService :PreviewService) { }

  ngOnInit() {
    this.sub= this.previewService.userTextObservable$.subscribe(
      x=>this.previewText=x
    );
  }

  ngOnDestroy():void{
    this.sub.unsubscribe();
  }
}