0

根据 NGXS 文档,以下是所有有效代码:

import { Select } from '@ngxs/store';
import { ZooState, ZooStateModel } from './zoo.state';

@Component({ ... })
export class ZooComponent {
  // Reads the name of the state from the state class
  @Select(ZooState) animals$: Observable<string[]>;

  // Uses the pandas memoized selector to only return pandas
  @Select(ZooState.pandas) pandas$: Observable<string[]>;

  // Also accepts a function like our select method
  @Select(state => state.zoo.animals) animals$: Observable<string[]>;

  // Reads the name of the state from the parameter
  @Select() zoo$: Observable<ZooStateModel>;
}

我的问题是关于第三个@Select。究竟,装饰器和/或框架如何知道应该在 lambda 中使用哪个状态?这里没有什么明显的说“这个组件使用 ZooState 作为它的状态对象”,那么如何确定要查看的正确状态切片呢?

作为记录,这种模式似乎确实工作得很好——我只是有点难过如何。

4

1 回答 1

1

当使用带有引用目标状态的函数的 NGXS@Select装饰器时,state这种情况下的 arg 表示整个状态对象,其中包含所有已注册状态的名称,这些状态在@State装饰器中提供(例如@State<ZooStateModel>({ name: 'zoo' }):)

例如,如果您有 3 个状态,定义为:zooapp、 和foo,并在商店中注册,那么statearg 的类型将如下所示:

@Select((state: { zoo: ZooStateModel; app: AppStateModel; foo: FooStateModel }) => state.zoo.animals)
animals$: Observable<string[]>;
于 2021-10-05T17:37:52.820 回答