乍一看,这似乎是 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 生命周期本身有关。
最佳答案
关于angular - NGXS:属性 'stream$' 在初始化之前使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65323439/