0

我是使用秋田状态管理的 Angular 新手。我正在尝试将值数组设置为实体状态。但由于某种原因,只有数组中的最后一项被设置。但是值的数量是正确的。IE。我在数组中有 14 个不同的项目。但在秋田,最后一个值设置为 14 次。不知道我在这里做错了什么。有人可以帮忙吗?

export interface UserTagState extends EntityState<string, string> {
  areTagsLoaded: boolean;
}

const initialState = {
  areTagsLoaded: false
};

@Injectable({
  providedIn: 'root'
})
@StoreConfig({ name: 'usertags' })
export class UserTagStore extends EntityStore<UserTagState> {
  constructor() {
    super(initialState);
  }
}

正面:

@Injectable({
  providedIn: 'root'
})
export class UserTagFacade {
  tags$ = this.query.tagsAreLoaded$
    .pipe(
      filter(areTagsLoaded => !areTagsLoaded),
      exhaustMap(areTagsLoaded => {
        if (!areTagsLoaded) {
          this.loadTags();
        }

        return this.query.tags$;
      }),
      shareReplay(1)
    );

  constructor(
    private readonly store: UserTagStore,
    private readonly query: UserTagQuery,
    private readonly api: UserTagDataService
  ) { }

  loadTags() {
    this.store.setLoading(true);
    this.api
      .getTags()
      .pipe(take(1))
      .subscribe((tags) => {
        this.store.setLoading(false);
        this.store.set(tags); // up until here the tags array shows the correct value
        this.store.update(state => ({
          ...state,
          areTagsLoaded: true
        }));
      });
  }
}

询问:

@Injectable({
  providedIn: 'root'
})
export class UserTagQuery extends QueryEntity<UserTagState> {
  tags$ = this.selectAll();

  tagsAreLoaded$ = this.select(state => {
    return state.areTagsLoaded;
  });

  constructor(protected store: UserTagStore) {
    super(store);
  }
}

输出:

Tags: [ "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics", "trader analytics" ]

谢谢

4

1 回答 1

0

在 loadTags() 中,删除下面的部分并再次运行。谢谢。

this.store.update(state => ({
    ...state,
    areTagsLoaded: true
}));
于 2021-10-11T09:34:00.410 回答