Pushing/nexting 来自可观察对象的值,未更新已订阅的可观察对象
Pushing/nexting a value from an observable subject, isn't updating a subscribed observable
我已经整理了一个我认为应该有效的简单示例..但它没有:(
我需要帮助来理解我做错了什么。
@Component({
templateUrl: 'sandbox.template.html'
})
export class SandBoxPage implements OnInit {
dataSubject: Subject<string> = Subject.create();
data$: Observable<string>;
constructor(private platform: Platform) {
this.data$ = this.dataSubject
.asObservable()
.startWith("First value in the observable");
}
onClick() {
console.log("onClick()");
this.dataSubject.next(Date.now + " value");
}
ngOnInit() {
console.log("ngOnInit()");
this.data$.subscribe((v) => {
console.log("new value", v);
}, (err) => {
console.log("Error", err);
});
}
}
我有一个连接到 onClick()
的按钮,但我订阅的 console.log 没有触发。它只在开始时以 startsWith
值触发一次。
如果您使用 create
创建主题,您需要根据文档为其提供 observer
和 observable
参数。
Rx.Subject.create(observer, observable)
解决方法:
只需使用 new
创建它
dataSubject: Subject<string> = new Subject<string>();
来源:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service
我已经整理了一个我认为应该有效的简单示例..但它没有:( 我需要帮助来理解我做错了什么。
@Component({
templateUrl: 'sandbox.template.html'
})
export class SandBoxPage implements OnInit {
dataSubject: Subject<string> = Subject.create();
data$: Observable<string>;
constructor(private platform: Platform) {
this.data$ = this.dataSubject
.asObservable()
.startWith("First value in the observable");
}
onClick() {
console.log("onClick()");
this.dataSubject.next(Date.now + " value");
}
ngOnInit() {
console.log("ngOnInit()");
this.data$.subscribe((v) => {
console.log("new value", v);
}, (err) => {
console.log("Error", err);
});
}
}
我有一个连接到 onClick()
的按钮,但我订阅的 console.log 没有触发。它只在开始时以 startsWith
值触发一次。
如果您使用 create
创建主题,您需要根据文档为其提供 observer
和 observable
参数。
Rx.Subject.create(observer, observable)
解决方法: 只需使用 new
创建它dataSubject: Subject<string> = new Subject<string>();
来源:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service