升级@ngrx/Store时,类型';操作';上不存在属性';有效负载'; [英] Property 'payload' does not exist on type 'Action' when upgrading @ngrx/Store
本文介绍了升级@ngrx/Store时,类型';操作';上不存在属性';有效负载';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的角度(4.x)应用程序中有@ngrx/store
包,并且正在从v2.2.2->v4.0.0升级。我可以看到迁移注释是这样写的:
然而,他们给出的例子似乎完全违反直觉(在我看来……)。已从操作接口中删除有效负载属性。
我有一个减速器函数,如下所示:
export function titleReducer(state = { company: 'MyCo', site: 'London' }, action: Action): ITitle {
switch (action.type) {
case 'SET_TITLE':
return {
company: action.payload.company,
site: action.payload.site,
department: action.payload.department,
line: action.payload.line
}
case 'RESET':
return {
company: 'MyCo',
site: 'London'
}
default:
return state
}
}
不出所料,现在引发打字错误:
[ts]类型‘Action’上不存在属性‘PayLoad’
但我从迁移指南中不知道这也应该改变什么。有什么想法吗?
推荐答案
您可以创建自己的操作类型,并定义payload
,请查看the example app以获取参考:
class AddBookAction implements Action {
readonly type = ADD_BOOK;
constructor(public payload: Book) {}
}
然后在the reducer中使用该类型:
function reducer(state = initialState, action: AddBookAction): State
可以像this那样调度操作:
this.store.dispatch(new AddBookAction(book));
还请注意,示例appcombines减速器可以包含到单个联合类型中的所有操作类型:
export type Actions =
| AddBookAction
| AddBookSuccessAction
export function reducer(state = initialState, action: Actions): State
这篇关于升级@ngrx/Store时,类型';操作';上不存在属性';有效负载';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文