【发布时间】:2019-09-19 02:36:12
【问题描述】:
我有一个服务->
subject = new Subject<any>();
constructor (){
this.subject.next('hello');
}
getsubject():Observable<any>{
return this.subject.asObservable();
}
和组件->
name:string;
subscription : Subscription;
constructor(private serv:SService) {
this.subscription=this.serv.getsubject().subscribe(message=>{console.log('hello');});
}
如您所见,我正在将 hello 消息从服务传递给订阅者,但在组件中订阅者没有获得该值,即使它没有记录任何单词。
plunkerexample
【问题讨论】:
-
尝试使用
next从subject获取值 -
@N1gthm4r3 看到这个关于问题的答案就像你的问题stackoverflow.com/a/43374509 可以解决这个问题