如果你是很新的可观,我建议你开始一个基本的例子。
例如:(伪代码,也许)
let observable = new Observable<string>(observer => {
if (1 == 1) {
observer.next("one is one");
} else {
observer.error("one is not one");
}
});
observable.subscribe(
response => console.log(response),
error -> console.log(error)
);
使用主题更多的是相同的,但一个主题,可以多一点自由使用:
let subject = new Subject();
subject.subscribe(response -> console.log(response));
subject.next('hello');
我相信我看到你把这两者结合起来,订阅这个主题,然后用可观察数据获取数据?通过这些例子,你是否有足够的信息,或者你需要一个非常具体的例子?
编辑:
您this.initialized.subscribe
'启动过程'。 你调用loadOtherData函数的时间太晚了,这使得你在数据返回时没有订阅this.initialized。这就是为什么你没有看到的console.log(“初始化”)
EDIT2:
注意到,一个BehaviorSubject
持有它的数据,并返回它的订阅。没有必要用空字符串来初始化它。
那么,如果我有一个可观察/订阅的参考,那么是否有一种方法来“订阅”已经完成的可观察项? – Gillardo
就像我说过的,BehaviorSubject返回它的订阅数据。所以这是一个可行的方法。 – Carsten