Code: Select all
// toast.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class ToastService {
toasts: { message: string; duration: number; type: 'success' | 'error' }[] = [];
add(message: string, duration: number = 3000, type: 'success' | 'error' = 'success') {
this.toasts.push({ message, duration, type });
setTimeout(() => this.remove(0), duration);
}
remove(index: number) {
this.toasts.splice(index, 1);
}
}
später habe ich Signale verwendet und das Problem wurde gelöst
Code: Select all
@Injectable({
providedIn: 'root',
})
export class ToastService {
toasts = signal([]);
create(message: string, type: AlertType): void {
const toastItem: ToastInfo = { message, type };
this.toasts.update(t => [...t, toastItem]);
of(toastItem) //creating a observale
.pipe(delay(5000)) // inserting into pipe
.subscribe(()=>{
this.remove(toastItem) //emitting
})
}
remove(value: ToastInfo){
this.toasts.update(t => t.filter(item => item !== value));
console.log(this.toasts);
}
}
Mobile version