Angular signalStore and HttpResource

Discussão em 'Angular' iniciado por Kino1D, Março 24, 2025.

  1. Kino1D

    Kino1D Guest

    const apiUrl = environment.apiUrl;
    export const PublisherStore = signalStore(
    withState(initialState),
    withComputed(({fetchStatus, saveFetchStatus}) => ({
    fetchPending: computed(() => fetchStatus() === 'pending'),
    saveFetchPending: computed(() => saveFetchStatus() === 'pending'),
    })),
    withProps(() => ({
    _toastService: inject(ToastService),
    })),
    ...
    ...
    withMethods(() => ({
    _postOffer: resource({
    loader: async () => {
    const post = await fetch(`${apiUrl}/api/offers`);
    return await post.json();
    }
    })
    }) as MethodsDictionary),
    withMethods((store: any) => ({
    ...

    updateDetails: (details: any) => {
    patchState(store, {details});
    }
    })),

    withLogger(STORE_KEY),
    );


    that's my code,

    i want to call api using resource

    withMethods(() => ({
    _postOffer: resource({
    loader: async () => {
    const post = await fetch(`${apiUrl}/api/offers`);
    return await post.json();
    }
    })
    }) as MethodsDictionary),


    but getting error

    publisher-plots.component.html:2 ERROR RuntimeError: NG0203: rxResource() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with runInInjectionContext. Find more at https://angular.dev/errors/NG0203

    what do i doing wrong?

    When i change WithMethods to WithProps the request is sent on store init, but its not my case. I want to link store method to button in my component

    Continue reading...

Compartilhe esta Página