如何将NGRX操作传递给函数,因为它';typescript中的参数?
在Angular 7应用程序中,我有一个较大的if-else条件,在这里我使用状态管理器和rxjs,并且希望使代码更具可读性。在不同的情况下,我需要采取不同的行动。我决定通过将一些部分移动到可重用函数来减少if-else代码块,并需要将NGRX操作作为参数传递给这些函数。但是typescript抛出了一个错误:参数“xxx”隐式地具有“any”类型 问题是-我应该为动作定义什么类型?如setSomeDataAction:Action-不起作用 立即编码:如何将NGRX操作传递给函数,因为它';typescript中的参数?,typescript,rxjs,angular7,ngrx,Typescript,Rxjs,Angular7,Ngrx,在Angular 7应用程序中,我有一个较大的if-else条件,在这里我使用状态管理器和rxjs,并且希望使代码更具可读性。在不同的情况下,我需要采取不同的行动。我决定通过将一些部分移动到可重用函数来减少if-else代码块,并需要将NGRX操作作为参数传递给这些函数。但是typescript抛出了一个错误:参数“xxx”隐式地具有“any”类型 问题是-我应该为动作定义什么类型?如setSomeDataAction:Action-不起作用 立即编码: if (mobile) {
if (mobile) {
if (newVisitor) {
this.store.dispatch(SetMobileForNewVisitor(payload));
} else {
this.store.dispatch(SetMobileForExistingVisitor(payload));
}
} else {
if (newVisitor) {
this.store.dispatch(SetNotMobileForNewVisitor(payload));
} else {
this.store.dispatch(SetNotMobileForExistingVisitor(payload));
}
}
抛出typescript错误的代码
mobile ?
this.dispatchRemoveOne(SetMobileForNewVisitor, SetMobileForExistingVisitor, payload) :
this.dispatchRemoveOne(SetNotMobileForNewVisitor,SetNotMobileForExistingVisitor, payload);
private dispatchRemoveOne(newVisitorAction, existingVisitorAction, payload) {
newVisitor
? this.store.dispatch(newVisitorAction(payload))
: this.store.dispatch(existingVisitorAction(payload));
}
我试着说:
private dispatchRemoveOne(newVisitorAction: Action, existingVisitorAction: Action, payload: Payload[]) {
newVisitor
? this.store.dispatch(newVisitorAction(payload))
: this.store.dispatch(existingVisitorAction(payload));
}
为什么不把逻辑放在减速机上,看起来更自然there@FanCheung减速器应该很简单。我们不能在减速机内进行逻辑计算,这可能真的没有固定的答案,似乎在设备上有4个条件检查,如果在reducer中,代码会更干净。为什么不在效果上这样做呢?为什么不在reducer中做逻辑,看起来更自然there@FanCheung减速器应该很简单。我们不能在减速机内进行逻辑计算,这可能真的没有固定的答案,似乎在设备上有4个条件检查,如果在reducer中,代码会更干净。为什么不这样做呢?