【发布时间】:2021-07-23 01:41:16
【问题描述】:
我最近开始从事一个新的 Angular 项目,我发现在不相关的组件之间共享状态的常见实现是使用 rxjs Subject/BehaviorSubject 作为类的静态成员。
例如:
export class AbcService {
private static importantData: Subject<any> = new Subject();
static get importantData$(): Observable<any>{
return AbcService.importantData.asObservable()
}
}
用法:
@Component({
...
})
export class DefComponent {
constructor() {
AbcService.importantData$.subscribe(...)
}
}
我试图思考这种方法的优点,但我无法通过这样做找到任何重要的价值,而且它也与注入概念形成对比。
我错过了什么,将可观察对象声明为静态类成员有什么好处?
【问题讨论】:
-
我从来没有真正遇到过这个..老实说我也想不出直接的好处..?
标签: javascript angular typescript oop rxjs