import { Injectable } from '@angular/core'; import { MatSnackBar } from '@angular/material'; import { Router } from '@angular/router'; import { Subject, Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class MessageService { private subject = new Subject(); private keepAfterNavigationChange = false; constructor(private _snackBar: MatSnackBar, private router: Router) { } snackBar(msg: string) { this._snackBar.open(msg, "x", { duration: 3000, verticalPosition: "top", }); } success(message: string, duration: number = 5, keepAfterNavigationChange = false) { this.keepAfterNavigationChange = keepAfterNavigationChange; this.subject.next({ type: 'success', text: message, duration: duration }); } error(message: string, duration: number = 5, keepAfterNavigationChange = false) { this.keepAfterNavigationChange = keepAfterNavigationChange; this.subject.next({ type: 'error', text: message, duration: duration }); } getMessage(): Observable { return this.subject.asObservable(); } }