How to dynamically load an entity in withMethod of ngrx signal store without triggering NG0600?

Discussão em 'Angular' iniciado por Burckhardt Sébastien, Abril 22, 2025.

  1. I'm working with the new ngrx/signals store, and I need to dynamically fetch an entity if it's not already present in the store.

    Here's the method I'm using inside a withMethod block :

    getTeam(uri: string): Team | undefined {
    let team: Team | undefined = store.entityMap()[uri];
    if (!team) {
    patchState(store, { requestStatus: 'pending' });
    gatewayService.loadResource<Team>(uri).subscribe({
    next: t => {
    team = t;
    patchState(store, { requestStatus: 'fulfilled' }, addEntity(t, teamConfig));
    },
    error: (error) => patchState(store, { requestStatus: { error: error.message } }),
    });
    }
    return team;
    }


    This results in the following error: ERROR RuntimeError: NG0600: Writing to signals is not allowed in a computed.

    I understand that patchState triggers a signal write during a computed context, which is not allowed. What would be the proper pattern to lazily load an entity only when needed, without triggering this runtime error? Is there a recommended way to defer loading logic outside of computed execution context, while keeping it ergonomic to access in the store?

    Thanks!

    Continue reading...

Compartilhe esta Página