NgRx EntityState - 我们可以在状态对象上使用 EntityState 属性
NgRx EntityState - Can we use EntityState on a state object property
继续我进入 NgRx
的新旅程,我刚刚发现 EntityState
,如果我理解并尝试正确使用它,它涵盖了我所有的 "collection manipulation" 操作需要为我做!
我遵循了这个 excellent tutorial,有一个小的区别,我想(如果可能的话)使用 EntityState
作为我的状态对象的 属性,而不是将其扩展为整个状态对象,如链接示例中所示)。我这样做是为了匹配我现有的数据,并允许另一个列表 属性,它也是一个 EntityState
(如果可以这样使用,再一次)。
我已经替换了我的一个功能状态对象中的对象数组,但在使用 select.
时遇到了问题
我有一个简单的版本here。
在这个例子之后,状态是针对 feature1,为简单起见,我只是将其放入文件夹而不是单独的模块。
我的应用程序有现有的 类,我在其中序列化来自服务器的数据。
我已将这些放入 freature1.reducers.ts 文件中..
export class BookModel {
public id : string;
public title: string;
}
/** Serialised in from the backend */
export class LibraryBackendModel {
books: Array<BookModel>;
}
所以我声明了我要存储的状态的接口,就是这个Array<BookModel>
希望可以换成EntityState
所以,功能状态,我有以下内容(在同一个文件中)...
export interface LibraryState {
isOpen : boolean,
books: EntityState<BookModel> // <----- now using EntityState instead of an array
// Maybe other collections to come
}
我有一个动作 getLibrarySuccess
当新数据从服务器传入时触发,在 reducer 文件中,我使用辅助方法 updateBookList
它将使用所有不同的方法在 EntityAdapter
上进行更新(不包括在内,但那就是它们所在的位置)。
到目前为止,一切似乎都很好..
现在终于,(问题所在)在feature1.component.ts
,我想订阅select或getAllBooks
this.store.pipe(select(fromLibrary.getAllBooks),
).subscribe(
books => this.libaryModel.books = books
);
在这里,我可以看到 IDE 报告了一个问题:
错误是……
Type '(state: EntityState<BookModel>) => BookModel[]' is not assignable to type 'BookModel[]'.
看来我的 select 或...
有问题
export const getAllBooks = createSelector(
getFeature1State,
state => fromFeature1.getBooks
);
但我不确定如何解决这个问题,我做错了什么。 fromFeature1.getBooks
是对 EntityAdapter
中 booksAdapter.getSelectors().selectAll;
的引用。我假设我需要以某种方式使用 selector 从这个 EntityAdapter
来获取我的更新和状态,但我不确定。
我发现了我的问题,我只需要按下面的方式传入 state.books
...
export const getAllBooks = createSelector(
getFeature1State,
state => fromFeature1.getBooks(state.books) <--- pass in state.books
);
继续我进入 NgRx
的新旅程,我刚刚发现 EntityState
,如果我理解并尝试正确使用它,它涵盖了我所有的 "collection manipulation" 操作需要为我做!
我遵循了这个 excellent tutorial,有一个小的区别,我想(如果可能的话)使用 EntityState
作为我的状态对象的 属性,而不是将其扩展为整个状态对象,如链接示例中所示)。我这样做是为了匹配我现有的数据,并允许另一个列表 属性,它也是一个 EntityState
(如果可以这样使用,再一次)。
我已经替换了我的一个功能状态对象中的对象数组,但在使用 select.
时遇到了问题我有一个简单的版本here。
在这个例子之后,状态是针对 feature1,为简单起见,我只是将其放入文件夹而不是单独的模块。
我的应用程序有现有的 类,我在其中序列化来自服务器的数据。
我已将这些放入 freature1.reducers.ts 文件中..
export class BookModel {
public id : string;
public title: string;
}
/** Serialised in from the backend */
export class LibraryBackendModel {
books: Array<BookModel>;
}
所以我声明了我要存储的状态的接口,就是这个Array<BookModel>
希望可以换成EntityState
所以,功能状态,我有以下内容(在同一个文件中)...
export interface LibraryState {
isOpen : boolean,
books: EntityState<BookModel> // <----- now using EntityState instead of an array
// Maybe other collections to come
}
我有一个动作 getLibrarySuccess
当新数据从服务器传入时触发,在 reducer 文件中,我使用辅助方法 updateBookList
它将使用所有不同的方法在 EntityAdapter
上进行更新(不包括在内,但那就是它们所在的位置)。
到目前为止,一切似乎都很好..
现在终于,(问题所在)在feature1.component.ts
,我想订阅select或getAllBooks
this.store.pipe(select(fromLibrary.getAllBooks),
).subscribe(
books => this.libaryModel.books = books
);
在这里,我可以看到 IDE 报告了一个问题:
错误是……
Type '(state: EntityState<BookModel>) => BookModel[]' is not assignable to type 'BookModel[]'.
看来我的 select 或...
有问题export const getAllBooks = createSelector(
getFeature1State,
state => fromFeature1.getBooks
);
但我不确定如何解决这个问题,我做错了什么。 fromFeature1.getBooks
是对 EntityAdapter
中 booksAdapter.getSelectors().selectAll;
的引用。我假设我需要以某种方式使用 selector 从这个 EntityAdapter
来获取我的更新和状态,但我不确定。
我发现了我的问题,我只需要按下面的方式传入 state.books
...
export const getAllBooks = createSelector(
getFeature1State,
state => fromFeature1.getBooks(state.books) <--- pass in state.books
);