[英]Subscribe is not a function Angular
I am trying to return the value while subscribing and I am getting the value also but getting the error also 我试图在订阅时返回值,我也得到了值但也得到了错误
** Inside Service ** **内部服务**
getColumnValueg(): Observable<any>
{ console.log("service",this.colValueg);
return (this.colValueg);
}
** FormComponent ** ** FormComponent **
ngOnInit()
{
// perform the task of sending and receiving value.
console.log("inint form-lib component",this.UserFormArray);
this.UserFormArray = this.service.getFormDetails();
this.service.getColumnValueg().subscribe(data => this.g1 = data);
this.service.getColumnValueg().subscribe(data => this.lg1 = data);
this.service.getColumnValueg().subscribe(data => this.md1 = data);
console.log('userformarray',this.UserFormArray);
console.log(this.g1,this.lg1,this.md1);
}
but data is not getting called after that I called these service method from a button click event then it showed the value. 但是在我从按钮单击事件中调用了这些服务方法之后,数据没有被调用,然后显示了值。 I am getting the error to subscribe is not a function.
我收到错误订阅不是函数。 I tried using of, from it's not working.
我尝试使用of,因为它不起作用。 I am trying to fetch value on page load but i am not able to fetch data on page load
我正在尝试在页面加载时获取值,但无法在页面加载时获取数据
Edited 编辑
The problem is solved but the next problem raised is that I am not able to fetch the value on nginit 问题已解决,但提出的下一个问题是我无法在nginit上获取值
Observable
Observable
方法 getColumnValueg(): Observable<any>
{ console.log("service",this.colValueg);
return Observable.of(this.colValueg);
}
OR 要么
getColumnValueg(): Observable<any>
{ console.log("service",this.colValueg);
return of(this.colValueg);
}
ngOnInit()
{
// perform the task of sending and receiving value.
console.log("inint form-lib component",this.UserFormArray);
this.UserFormArray = this.service.getFormDetails();
this.service.getColumnValueg().subscribe(data => {
console.log(" ColumnValue data " , data);
});
console.log('userformarray',this.UserFormArray);
console.log(this.g1,this.lg1,this.md1);
}
Demo link is here - https://stackblitz.com/edit/angular-srabjg 演示链接在这里-https://stackblitz.com/edit/angular-srabjg
Looks like this.colValueg
is not an Observable. 看起来像这样
this.colValueg
不是Observable。 You'll have to convert it into one. 您必须将其转换为一个。 You can use
of
to do that: 您可以使用
of
做到这一点:
If you're using Rxjs6: 如果您使用的是Rxjs6:
import { of } from 'rxjs';
...
colValueg = 'Some Value';
...
getColumnValueg(): Observable < any > {
console.log("service", this.colValueg);
return of(this.colValueg);
}
If you're using Rxjs5: 如果您使用的是Rxjs5:
import { Observable } from "rxjs/Observable";
...
colValueg = 'Some Value';
...
getColumnValueg(): Observable < any > {
console.log("service", this.colValueg);
return Observable.of(this.colValueg);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.