2017-01-05 233 views
1

我已经放在一个简单的例子,我相信应该工作..但它不:( 我需要帮助了解我的'在做错误的。从可观察主题推/置值,不更新订阅可观察

@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值触发一次按钮。

回答

1

如果你创建了一个题目create,你需要给它observer和根据文档的参数。

Rx.Subject.create(观察员,观察到的)

来源:https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md#rxsubjectcreateobserver-observable

解决方案: 只是新

dataSubject: Subject<string> = new Subject<string>(); 

来源创建:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

+0

哇。只是......哇。多么微妙的头痛。谢谢! – Matt

+0

@Matt我们都在那里:) – echonax