可以在Akita Store中使用setEntireState()
从部分更新中排除 Akita 订阅者。
以下是一个例子:
import { Injectable } from '@angular/core';
import { Store, StoreConfig } from '@datorama/akita';
import { User } from './user.model';
export interface UsersState {
users: User[];
selectedUserId: string | null;
}
export function createInitialState(): UsersState {
return {
users: [],
selectedUserId: null
};
}
@Injectable({ providedIn: 'root' })
@StoreConfig({ name: 'users' })
export class UsersStore extends Store {
constructor() {
super(createInitialState());
}
updateSelectedUserId(id: string) {
this.update(state => ({ selectedUserId: id }), { partial: true });
}
updateUsers(users: User[]) {
// 'setEntireState' to exclude subscribers from partial updates
this.setEntireState(state => ({ users }));
}
}
在updateUsers()
方法中,使用setEntireState()
以避免订阅者从部分更新中接收新状态。