Rxjs Angular6-变量不会更新

Rxjs Angular6-变量不会更新,rxjs,angular6,Rxjs,Angular6,很抱歉,我确定这是关于angular最常问的问题之一,我发现了很多问题,但没有任何帮助。我用一个变量得到了一个简单的可观察-订阅关系 我的服务: activeRoom: number = 0; // we're starting in room 0 /* get the roomnumber from open-rooms to chat-window */ getActiveRoom(): Observable<number> { return of(this.activeR

很抱歉,我确定这是关于angular最常问的问题之一,我发现了很多问题,但没有任何帮助。我用一个变量得到了一个简单的可观察-订阅关系

我的服务:

  activeRoom: number = 0; // we're starting in room 0

/* get the roomnumber from open-rooms to chat-window */
getActiveRoom(): Observable<number> {
return of(this.activeRoom);
}
很明显,我不能每次单击都更新我的组件,所以选项b不是选项D 我在这里遗漏了什么显而易见的东西

这里有一个有效的例子

服务:

private userArray: User[] = [];
  getUserArray(): Observable<User[]>{
  return of(this.userArray);
 }
有人能用一个非常大的红色箭头指向我在这里丢失的东西吗?

 activeRoom: number = 0; 
 getActiveRoom(): Observable<number> { 
 return of(this.activeRoom); 
 } 

 setActiveRoom(roomID): void {
  this.activeRoom = roomID; 
 }
致:


它是有效的。老实说,我不知道为什么或者什么发生了变化,但我很高兴它能起作用:D

我无法理解这是您的问题变量this.activeRoom在服务中更新时不会在组件中更新。我可以肯定地说,服务会更新组件,因为我可以通过this.activeRoomService.activeRoom直接访问它。因此,断开的链接位于订阅和getter之间。如果打印newActiveRoom,它包含哪些内容?似乎您遗漏了一些代码,activeRoom在哪里更新?不完全确定您的问题是否正确。newActiveRoom是getActiveRoom函数的返回语句。函数本身返回一个可观察值,但在语句中,newActiveRoom是数字本身,然后传输到this.activeRoom。例外:不是。activeRoom的setter不是问题所在,因为它使用的是直接链接,但是我可以告诉您两行代码:setActiveRoomId:void{this.activeRoom=roomID;}那么newActiveRoom总是0?
constructor(private backend: BackendService) {}
ngOnInit() {
    this.backend.getUserArray().subscribe(user => {
        this.userlist = user;
    //    console.log(user);
    });
 activeRoom: number = 0; 
 getActiveRoom(): Observable<number> { 
 return of(this.activeRoom); 
 } 

 setActiveRoom(roomID): void {
  this.activeRoom = roomID; 
 }
activeRoom: Subject<number> = new Subject; 
public getActiveRoom(): Observable<number> { 
 return this.activeRoom.asObservable(); 
} 

public setActiveRoom(roomID): void { 
 this.activeRoom.next(roomID); 
}