angular - NGXS:属性 'stream$' 在初始化之前使用

标签 angular typescript ngxs

乍一看,这似乎是 Angular 组件生命周期逻辑的默认行为,因为组件属性尚未初始化,因此在组件初始化完成之前,stream$ 无法进行赋值:

@Component({
  selector: 'app-component',
  template: `<p>app works!</p>`,
})
export class AppComponent implements OnInit {
  @Select(AppState.getItems)
  stream$: Observable<string[]>;

  firstItemStream$: Observable<{ selectedItem: string }> = this.stream$ // error occurs here
  .pipe(
    find(({ itemId }) => itemId === 0),
  );

  ...

引自 NgXs:

APP_INITIALIZER is resolved after NGXS states are initialized. They are initialized by the NgxsModule that is imported into the AppModule. The ngxsOnInit method on states is also invoked before the APP_INITIALIZER token is resolved.

上面的代码按预期工作,但 typescript 错误仍然发生。有什么解决办法吗?

此外,我决定不对 typescript 开源项目提出问题,因为它也可能与 NgXs 生命周期本身有关。

最佳答案

听起来像this使用选择装饰器的场景。

因此您需要将声明更新为:

@Select(AppState.getItems) stream$!: Observable<string[]>;

关于angular - NGXS:属性 'stream$' 在初始化之前使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65323439/

相关文章:

angular - 需要帮助以了解我如何正确实现订阅

typescript 编译文件找不到模块

unit-testing - ngxs:如何测试动态选择器

angular - 如何从数组列表中删除数据 - NGXS 状态管理

NGXS 异步操作订阅

javascript - HighCharts:删除网格线悬垂(Angular/TypeScript)

javascript - 使用 RXJS 进行搜索输入,该输入进行 API 调用,但订阅会重复发生,即使使用 debounceTime

javascript - typescript 返回对象或数组错误

casting - typescript :在评论中设置变量类型

node.js - 将枚举与 TypeScript 结合使用,并在每个枚举上使用方法