Yon*_*n B 12 rxjs angular2-services ngrx angular
我正在订阅组件构造函数中的store属性
constructor(private someService: SomeService){
someService.someObservable.subscribe((data: Some)=> {
this.some = data;
console.log('changed');
});
}
Run Code Online (Sandbox Code Playgroud)
在服务中,构造函数如下所示:
constructor(private store: Store<any>){
this.someObservable = <Observable<{}>>store.select('some');
}
Run Code Online (Sandbox Code Playgroud)
我一直在改变这个对象的状态,但是日志只出现一次.还有一点奇怪的是,我正在this.some
我的模板中直接访问该属性(没有async
管道),它完全更新!看起来该this.some
属性正在更新,但next()
订阅中的功能不起作用.
帮助任何人?谢谢!
您不应该在构造函数中调用服务,这被认为是一种不好的做法。使用 ngOnInit() 方法订阅服务。该方法会在组件加载时自动调用。
尝试这样,这可能会解决你的问题。
import {Component, OnInit} from "@angular/core";
export class YourComponent implements OnInit{
ngOnInit():void {
//handle your subscription here
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4136 次 |
最近记录: |