还有其他更干净的方法可以通过操作(NGXS)从http服务返回值吗?还是我们应该总是更新“结果”状态,以便在您返回时进行咨询?
下面的代码工作,非常接近于文档。然而,在我看来,它并不是很干净。
export class DeleteAction {
static readonly type = '[Test] Delete';
constructor(public id: number) { }
}
@State<TestStateModel>({
name: 'test',
defaults: {
result: null
}
})
export class TestState {
constructor(
private testService: TestService) {
}
@Selector()
static getResult(state: TestStateModel): NotificationResult {
return state.result;
}
@Action(DeleteAction)
public delete({ patchState }: StateContext<TestStateModel>, { id }: DeleteAction) {
return this.testService
.delete(id)
.pipe(
take(1),
tap(result => {
patchState({ result });
})
);
}
@Component({
templateUrl: './test.component.html'
})
export class TestComponent {
@Select(SubjectState.getResult) result$;
private delete(id: number): void {
this.isLoading = true;
this.store.dispatch(new DeleteAction(id))
.pipe(withLatestFrom(this.result$))
.subscribe(([_, r]) => {
const result = r as NotificationResult;
if (result.isValid) {
// ...
} else {
// ...
}
this.isLoading = false;
});
}
}
export interface NotificationResult {
isValid: boolean;
messages: NotificationMessage[];
}
由于除了调用"Delete“方法之外,我不会使用”结果“,因此似乎没有必要为此目的创建一个状态。
还有其他更优雅的方式吗?
发布于 2019-09-07 11:05:24
如果不将删除结果链接到调度,则会更干净。相反,目前采用的删除方法是:
你可以:
这在我看来更干净。有什么想法?
https://stackoverflow.com/questions/57827267
复制