79816441

Date: 2025-11-11 09:13:37
Score: 3
Natty:
Report link

I have the same issue, I have a dependency on an observable and I wanted to call toSignal in a way that it's not in a reactive context, so basically I wanted to have a withMethods with a private method returning the signalified observable to consume in the computed. There are some workaround but I think the methods defined in the withMethods should be available in the withComputed, there is plenty of other ways to do stupid things, this restriction is completely unnecessary.

So workaround 1:

withComputed(store => {
      const yourMethod = () => { };
      return {
        yourComputed: computed(() => store.yourState() * yourMethod()),
      };
    }),

This works fine, but the method is only reusable if you define it as a function outside of the signalStore.

Workaround 2:

withComputed((store, yourService = inject(YourService)) => ({
      yourComputed: computed(() => store.yourState() * yourService.yourMethod()),
    })),

By extracting your dependencies to a separate injectable you can reuse the methods and it looks a bit nicer also. In this case the YourService needs to be provided (I mean a mocked version) if you are using the createSignalStoreMock from ngx-signals-plus (this is also true for the first workaround if that contains injection).

Reasons:
  • Blacklisted phrase (1): I have the same issue
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I have the same issue
  • Low reputation (1):
Posted by: dszendrei