NGXS 状态文档
NGXS State documentation
我是 NGXS 的新手,我正在尝试完全理解这些文档,这样我就可以开始使用它了。
here.
的这段代码片段中有一件事我不明白
export class ZooState {
constructor(private animalService: AnimalService) {}
@Action(FeedAnimals)
feedAnimals(ctx: StateContext<ZooStateModel>, action: FeedAnimals) {
return this.animalService.feed(action.animalsToFeed).pipe(tap((animalsToFeedResult) => {
const state = ctx.getState();
ctx.setState({
...state,
feedAnimals: [
...state.feedAnimals,
animalsToFeedResult,
]
});
}));
}
}
就在这段代码的下方,它说:
You might notice I returned the Observable and just did a tap. If we
return the Observable, the framework will automatically subscribe to
it for us, so we don't have to deal with that ourselves. Additionally,
if we want the stores dispatch function to be able to complete only
once the operation is completed, we need to return that so it knows
that.
框架将订阅 this.animalService.feed
,但为什么呢?
FeedAnimals 操作使用注入的服务 AnimalService 来喂养在操作负载中传递的动物。据推测,该服务是异步运行的,并且 returns 是一个 Observable。该 Observable 的值通过 tap 函数访问,并用于在成功完成的基础上更新 ZooState 状态上下文。
为了专门和Angular一般使用 NGXS,您真的必须了解 RxJS...here's my goto doc page for it
我是 NGXS 的新手,我正在尝试完全理解这些文档,这样我就可以开始使用它了。
here.
的这段代码片段中有一件事我不明白export class ZooState {
constructor(private animalService: AnimalService) {}
@Action(FeedAnimals)
feedAnimals(ctx: StateContext<ZooStateModel>, action: FeedAnimals) {
return this.animalService.feed(action.animalsToFeed).pipe(tap((animalsToFeedResult) => {
const state = ctx.getState();
ctx.setState({
...state,
feedAnimals: [
...state.feedAnimals,
animalsToFeedResult,
]
});
}));
}
}
就在这段代码的下方,它说:
You might notice I returned the Observable and just did a tap. If we return the Observable, the framework will automatically subscribe to it for us, so we don't have to deal with that ourselves. Additionally, if we want the stores dispatch function to be able to complete only once the operation is completed, we need to return that so it knows that.
框架将订阅 this.animalService.feed
,但为什么呢?
FeedAnimals 操作使用注入的服务 AnimalService 来喂养在操作负载中传递的动物。据推测,该服务是异步运行的,并且 returns 是一个 Observable。该 Observable 的值通过 tap 函数访问,并用于在成功完成的基础上更新 ZooState 状态上下文。
为了专门和Angular一般使用 NGXS,您真的必须了解 RxJS...here's my goto doc page for it