无法读取未定义的属性(读取 'subscribe')

Cannot read properties of undefined (reading 'subscribe')

我一直在尝试订阅一个特定的可观察对象(我是 rxjs 的初学者),但我一直失败。 这是我的代码


这里我需要将usergroups数组元素的第一个元素赋值给this.currentGroup

const selectedUserGroups$ =
this.userGroupsService.getUserGroupsWithUsers(this.selectedUserGroupIds).pipe(shareReplay());

this.selectedUserGroups$.subscribe( (usergroups: UserGroup[]) =>
this.currentGroup = usergroups[0]); 

if(this.currentGroup.users?.length >0) 
{
  this.showUsersList(); 
}

但这不会像我预期的那样工作,感谢您对此的帮助

const selectedUserGroups$ = // assignment here
this.selectedUserGroups$.subscribe( //...

这是两个不同的对象:函数作用域中的变量 selectedUserGroups$ 和对象 属性 this.selectedUserGroups$.

例如,

从第二行删除 this.。或将 const selectedUserGroups$ = 替换为 this.selectedUserGroups$ =.