可观察的已订阅但不返回值

时间:2019-07-26 01:19:57

标签: angular subscribe

我正在尝试将Subscription的值传递给其他组件。根据控制台日志,我可以正常使用服务,但不能从服务发送给类?

从初始页面加载开始的控制台日志:

  

在home.component中观看lg sub

     

svc推送可观察

     

lg已更新为首页:0

     

svc设置lg:0

当我更改值时,我得到:

  

svc设置lg:2

我也希望看到这一点,但是没有:

  

lg已更新为首页:2

我的服务(接收更新的值并更新订阅)

....
export class HeaderService {

    private _selectedLgSub = new BehaviorSubject<LicenseGroup>(new LicenseGroup());

    public _selectedLgObservable() {
        console.log('svc pushed observable') //happens once
        return this._selectedLgSub.asObservable();
    }

    public setLg(lg: LicenseGroup) {
        console.log('svc set lg: ' + lg.id)
        this._selectedLgSub.next(lg);
    }

}

我的组件(已订阅该值):

export class HomeComponent implements OnInit{
    private _subLg: Subscription = new Subscription();
    constructor(public auth: AuthService, private router: Router, private headSvc: HeaderService) { }

    ngOnInit() {
        console.log('watching lg sub in home.component')
        this._subLg = this.headSvc._selectedLgObservable().subscribe(lg => console.log('lg updated for home: ' + lg.id));
    }
    .....

0 个答案:

没有答案