从商店获取价值时遇到问题。操作挂起。我尝试通过以下方式获取数据:
async onInit(params: Params) {
// it hangs on the line bellow
var userToken = await this.store.select(fromRoot.getUserToken).toPromise();
console.log(userToken);
}
Run Code Online (Sandbox Code Playgroud)
然后我执行以下异步函数:
ngOnInit(): void {
this.route.params.subscribe(params => {
this.onInit(params).then(() => console.log('promise executed'));
});
Run Code Online (Sandbox Code Playgroud)
永远不会返回用户令牌。
如果我订阅了商店,那么它工作正常。
this.store.select(fromRoot.getUserToken).subscribe(ut => {
console.log(ut);
});
Run Code Online (Sandbox Code Playgroud)
我最近也遇到了这个问题。正如 @maxime1992 指示您需要一个运算符来结束可观察的。
async onInit(params: Params) {
// it hangs on the line bellow
const userToken = await this.store.select(fromRoot.getUserToken).pipe(take(1)).toPromise();
console.log(userToken); // ^^^^^^^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud)
rxjs 7+ 更新,现在我们使用firstValueFrom 或lastValueFrom:
async onInit(params: Params) {
// it hangs on the line bellow
const userToken = await firstValueFrom(this.store.select(fromRoot.getUserToken).pipe(take(1)));
console.log(userToken); // ^^^^^^^^^^^^ ^
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1275 次 |
| 最近记录: |