Создание многоразовой снэк-панели (angular 7), которая может отображать разные сообщения, полученные от разных компонентов, используя сервис обмена данными. Snackbar отображается только при перезагрузке, когда я нажимаю на кнопку, и показывает все сообщения сразу.
app \ snackbar \ snackbar.component. html
<div class="show-snackbar">
<div *ngIf="showSnackbar"
[@fadeInFadeOutAnimation]>
<div *ngIf="add" class="snackbar">
<p>{{ MessageAdded }}</p>
<button class="btn-text" type="button"
(click)="onClose($event)">
Close
</button>
</div>
<div *ngIf="remove" class="snackbar">
<p>{{ MessageRemoved }}</p>
<button class="btn-text" type="button"
(click)="onClose($event)">
Close
</button>
</div>
</div>
app \ snackbar \ snackbar.component.ts
import { Component, OnInit } from '@angular/core';
import { fadeInFadeOutAnimation } from '../shared/animations';
import { SnackbarService } from 'app/services/snackbar.service';
const MSG_TIMEOUT = 5000;
@Component({
selector: 'snackbar-ce',
templateUrl: './snackbar.component.html',
styleUrls: ['./snackbar.component.scss'],
animations: [
fadeInFadeOutAnimation,
],
})
export class SnackbarComponent implements OnInit {
showSnackbar;
add;
remove;
constructor(private snackbarService: SnackbarService) { }
ngOnInit() {
this.showSnackbar = this.snackbarService.showSnackbar;
this.add = this.snackbarService.add;
this.remove = this.snackbarService.remove;
setTimeout(() => {
this.showSnackbar = false;
}, MSG_TIMEOUT);
}
ngAfterViewInit() { }
onClose(e: Event) {
this.showSnackbar = false;
}
}
app \ services \ snackbar.service.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SnackbarService {
private showSnackbarSource = new Subject<any>();
public showSnackbar = this.showSnackbarSource.asObservable();
private addSource = new Subject<any>();
public add = this.addSource.asObservable();
private removeSource = new Subject<any>();
public remove = this.removeSource.asObservable();
constructor() { }
changeShowSnackbar(showSnackbar: boolean) {
this.showSnackbarSource.next(showSnackbar);
}
showAddMessage(added: boolean) {
this.addSource.next(added);
}
showRemoveMessage(removed: boolean) {
this.removeSource.next(removed);
}
}
app \ shared \ header \ header.component. html
<div class="header">
<div class="btn"
[ngClass]="{
'active': addNew,
'inactive': !addNew
}"
(click)="toggleAddRemove($event)">
<i class="icon-like"></i>
</div>
</div>
app \ shared \ header \ header.component.ts
import { Component, OnInit, Inject } from '@angular/core';
import { SnackbarService } from '../../services/snackbar.service';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
addNew = false;
showSnackbar = false;
add = false;
remove = false;
constructor(private snackbarService: SnackbarService) { }
ngOnInit() {
this.snackbarService.showSnackbar.subscribe(result => { this.showSnackbar = result; });
this.snackbarService.add.subscribe(result => { this.add = result; });
this.snackbarService.remove.subscribe(result => { this.remove = result; });
}
toggleAddRemoveFavorites(e: Event) {
this.addNew = !this.addNew;
this.snackbarService.changeShowSnackbar(true);
if (this.addNew) {
this.snackbarService.showAddMessage(true);
this.snackbarService.showRemoveMessage(false);
console.log("isAdded");
} else {
this.snackbarService.showAddMessage(false);
this.snackbarService.showRemoveMessage(true);
console.log("isRemoved");
}
this.snackbarService.changeShowSnackbar(false);
}
}