Class NGXS 中状态的基于对象实例
Class based object instances for state in NGXS
NGXS Style Guide有一点:
It is not recommended to add Class based object instances to your state because this can lead to undefined behavior in the future.
例如
//== This is NOT recommended:
ctx.setState((state: Todo[]) => state.concat(new Todo(action.title)));
//== This is recommended
state.concat({ title: action.title, isCompleted: false })
为什么不建议使用 new
创建 class 实例?
因为那些创建的对象有多个属性,比如函数、构造函数,也许还有私有属性
等等 ...
您也不想保存这些属性!
你的待办事项中应该有导出功能-class:
interface TodoModel {
/* your properties you want to take care of! */
}
class Todo implements TodoModel {
export(): TodoModel {
}
import(model: TodoModel) {
}
}
NGXS Style Guide有一点:
It is not recommended to add Class based object instances to your state because this can lead to undefined behavior in the future.
例如
//== This is NOT recommended:
ctx.setState((state: Todo[]) => state.concat(new Todo(action.title)));
//== This is recommended
state.concat({ title: action.title, isCompleted: false })
为什么不建议使用 new
创建 class 实例?
因为那些创建的对象有多个属性,比如函数、构造函数,也许还有私有属性 等等 ... 您也不想保存这些属性!
你的待办事项中应该有导出功能-class:
interface TodoModel {
/* your properties you want to take care of! */
}
class Todo implements TodoModel {
export(): TodoModel {
}
import(model: TodoModel) {
}
}