【问题标题】:Angular Subject.Next Not Being DetectedAngular Subject.Next 未被检测到
【发布时间】:2017-06-20 20:02:20
【问题描述】:

我有以下几段代码。我可以看到前两部分似乎工作正常,但对变量变化做出反应的第三部分从未发生过。有什么想法吗?

服务

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class ConversationService {
    private convoIdSource = new Subject<number>();

    convoIdChanged = this.convoIdSource.asObservable();

    changeConvo(newConvoId: number){        
        this.convoIdSource.next(newConvoId);
    }
}

变化

openTeamChat(chatId: number){
    this.convoService.changeConvo(chatId);
}

假设的检测

convoService.convoIdChanged.subscribe(convoId => {
    console.log(convoId);
    this.convoId = convoId;
});

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    原来我有两个不同的服务实例,所以它们不同步。我不得不将提供者声明一直移到 app.module.ts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 2019-08-25
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多