Angular 19, WriteableSignal for array which dynamically changed

Discussão em 'Angular' iniciado por L. Kvri, Dezembro 25, 2024.

  1. L. Kvri

    L. Kvri Guest

    I have a writeable signal in a service:

    private errorEntries: WritableSignal<ErrorEntry[]> = signal<ErrorEntry[]>([])


    I have a getter in the service:

    getErrorEntries(): WritableSignal<ErrorEntry[]> {
    return this.errorEntries;
    }


    and anode methode:

    public addError(error: any): void {
    const timestamp = new Date().toISOString();
    const message = error.message || error.toString();
    const stack = error.stack || 'No stack trace available';
    const route = this.router.url;

    const errorEntry: ErrorEntry = { timestamp, message, stack, route };
    this.errorEntries.update((entries) => [...entries, errorEntry]);
    }


    I have an effect in another component.

    showExclamationMark = false;

    constructor(private globalErrorHandlerService: GlobalErrorHandlerService) {
    effect(() => {
    const errors = this.globalErrorHandlerService.getErrorEntries()();
    this.showExclamationMark = errors.length > 0;
    });
    }


    The effect works fine. But the errors.length always empty, why? I don't see what I missed.

    I noticed during debug in the service one of the errorEntries property has a value:

    SYMBOL(Signal):
    value: Array(1)


    contains a value.

    Continue reading...

Compartilhe esta Página