1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Subscribe or callback on Signal store method?

Discussão em 'Angular' iniciado por Tomasz Piątek, Maio 23, 2025.

  1. I want to make a redirect after executing API request (on success) in signal store method.

    I have 3 "solutions":

    1. Use callback, but I don't feel it.
    2. Change signal on next and then in component convert signal to observable and simple subscribe:

    deleteById: rxMethod<any>(
    pipe(
    switchMap((id: string) => {
    return _service.deleteID(id).pipe(
    tapResponse({
    next: () => {
    patchState(store, {
    status: 'success',
    currentSelectedId: undefined,
    });
    },
    ...
    }),
    );
    }),
    ),
    ),


    And then in the component:

    #currentSelectedId$ = toObservable(this.#store.currentSelectedId);
    ...
    this.#currentSelectedId$
    .pipe(
    filter(id => id === undefined),
    takeUntilDestroyed(this.#destroyRef),
    )
    .subscribe(() => {
    this.#router.navigate(['../'])
    });

    1. Don't use rxMethod and just return Observable from the method and subscribe to it in the component:

    deleteById: (id) => {
    return _service.deleteID(id);
    }


    and component:

    this.store.deleteById(id).subscribe(....);


    How do you resolve similar cases?

    I thought also about using withHook and effect on onInit, but my main goal is not to trigger any side effect in the store (like redirect).

    Continue reading...

Compartilhe esta Página