import { Injectable , Optional , SkipSelf } from '@angular/core' ; import { Subject } from 'rxjs' ; export interface ToastMessage { message : string ; } @ Injectable ( { providedIn : 'root' } ) export class ToastService { private toastSubject = new Subject < ToastMessage > ( ) ; toastState = this . toastSubject . asObservable ( ) ; constructor ( @ Optional ( ) @ SkipSelf ( ) prior ) { if ( prior ) { console . log ( 'toast service already exists' ) ; return prior ; } else { console . log ( 'created toast service' ) ; } } activate ( message ? ) { this . toastSubject . next ( < ToastMessage > { message : message } ) ; } }