使用 signalStore 和 rxMethod 创建实体

问题描述 投票:0回答:1

我有一条我理解的错误消息,但我不知道如何解决它。 “add”调用无需 HTTP 和“rxMethod”即可工作。但是,无法在构造函数之外加载此方法。我做错了什么?

addEntity: (entityToPush: entity) => {
          return rxMethod(pipe(
            tap(() => patchState(entityStore, {isLoading: true})),
            switchMap(() => entityHttpService.createEntity(entityToPush).pipe(
              tapResponse({
                next: (entityToPush) => patchState(entityStore, {entities: [...entityStore.entities(), entityToPush]}),
                error: console.error,
                finalize: () => patchState(entityStore, {isLoading: false})
              })
            ))
          ));

错误:rxMethod()只能在注入上下文中使用,例如构造函数、工厂函数、字段初始值设定项或与

一起使用的函数
angular signals ngrx store angular17
1个回答
0
投票

你使用的方法不对。应该是:

addEntity: rxMethod<entity>(pipe(
 // ...       
));

按照此操作:https://ngrx.io/guide/signals/signal-store

© www.soinside.com 2019 - 2024. All rights reserved.