带有 fork-join 的 Angular 订阅方法不起作用

发布于 2025-01-11 18:09:52 字数 1106 浏览 0 评论 0 原文

这是我的许可服务。我想要一个布尔数组。

import { forkJoin, Observable } from "rxjs";
import { map } from 'rxjs/operators';                                                   
           

private hasPermission(lessorId: number, userId: number, permission: string): boolean {
 // returns boolean. It works correctly
}

hasPermissionsForX(permissions: string[]): Observable<boolean[]> {
return forkJoin([
  this.userService.getUser(), //returns Observable
  this.lessorService.getLessor() //returns Observable
]).pipe(
  map(([user, lessor]) => permissions.map(permission => this.hasPermission(lessor.lessorId, user.userId, permission)))
)}

但是,我的订阅方法不起作用。该方法尚未进入该函数。我什至无法得到 console.log 响应。这是我的订阅方法。

const permissionsToCheck = ['firstPermission', 'secondPermission', 'thirdPermission'];
this.permissionService.hasPermissionsForX(permissionsToCheck).subscribe(([firstPermission, secondPermission, thirdPermission]) => {
  console.log(firstPermission, secondPermission, thirdPermission)                              
})

我该怎么办?我做错了什么?

提前致谢!

This my permission service. I wanna get a boolean array.

import { forkJoin, Observable } from "rxjs";
import { map } from 'rxjs/operators';                                                   
           

private hasPermission(lessorId: number, userId: number, permission: string): boolean {
 // returns boolean. It works correctly
}

hasPermissionsForX(permissions: string[]): Observable<boolean[]> {
return forkJoin([
  this.userService.getUser(), //returns Observable
  this.lessorService.getLessor() //returns Observable
]).pipe(
  map(([user, lessor]) => permissions.map(permission => this.hasPermission(lessor.lessorId, user.userId, permission)))
)}

However, my subscription method is not working. The method had not been getting step into the function. I can't even get console.log response. This is my subscription method.

const permissionsToCheck = ['firstPermission', 'secondPermission', 'thirdPermission'];
this.permissionService.hasPermissionsForX(permissionsToCheck).subscribe(([firstPermission, secondPermission, thirdPermission]) => {
  console.log(firstPermission, secondPermission, thirdPermission)                              
})

What am I supposed to do? What am I doing wrong?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2025-01-18 18:09:52

从你的服务方法的名称来看,我假设你实际上想使用在所有 Observables emit 之后发出的 zip ,而不是 forkJoin > 仅当所有源可观察对象完成后才会发出。

另外,请记住,每个可观察对象都需要订阅者才能发出任何内容。因此,请确保您确实订阅了 hasPermissionsForX() 的结果。

zip

forkJoin

Judging by the name of your service methods, I'd assume that you actually wanted to use zip which emits after all Observables emit, instead of forkJoin which will only emit once all source observables complete.

Also, keep in mind that every observable needs a subscriber in order to emit anything. So, make sure that you actually subscribe to the result of hasPermissionsForX().

zip

forkJoin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文