angular - Angular Observable 返回订阅者而不是 JSON 并且字符串显示未定义且没有结果
问题描述
我在 as 中定义的ngOnInitapp.component.ts
函数。
async ngOnInit(){
await this.api.GetSwapi()
await this.api.mySubject.subscribe(data => console.log); //Not getting data in console log
await this.api.mySubject.subscribe((data: any) => console.log); //Not getting data in console log
console.log('---------------------123123123-----------------------');//working
await this.api.GetSwapi2().subscribe(j => console.log(j)); //Getting undefined two times
await this.api.testSubject.subscribe(data => console.log); //Not getting data in console log
}
这是我的api.service.ts
。
export class ApiService {
constructor(private http: HttpClient) { }
mySubject = new Subject();
testSubject = new Subject<string>();
GetSwapi(){ //FUNCTION 1
this.http.get<any>('https://swapi.dev/api/people/')
.subscribe(data => {
console.log(data.results);
this.mySubject.next(data.result);
});
}
GetSwapi2(): Observable<any> { //FUNCTION 2
this.http.get<any>('https://swapi.dev/api/people/')
.subscribe(data => {
this.mySubject.next(data.result);
});
this.testSubject.next('woooow');
return this.mySubject.asObservable();
}
}
只有这个显示undefined两次await this.api.GetSwapi2().subscribe(j => console.log(j));
其他人看不到console.log
我只是想在我的app.component.ts
.
解决方案
发现我的答案是一个小错误。
GetSwapi(){ //FUNCTION 1
this.http.get<any>('https://swapi.dev/api/people/')
.subscribe(data => {
console.log(data.results);
this.mySubject.next(data.result);
});
}
问题在下一个..this.mySubject.next(data.result);
它results
不像显示的结果result
那样。console.log(data.results)
推荐阅读
- python - ValueError:推断后无法确定某些类型(pyspark)
- fstar - 具有类型参数的互归纳数据类型
- bootstrap-4 - 按钮上的禁用按钮属性不让模式触发?
- c# - ASPNet Entity Framework 6 - EF6,在同一个工作单元中混合异步和同步
- jquery - 使用 Datatables 呈现时更改呈现的列数
- javascript - 在数组中查找唯一对象
- r - 如何为ggplot2中的不同方面定义不同的y轴中断?
- sql - 如何使用 FOR 循环向数组添加值
- r - 所有列都满足一个标准的行索引
- allure - 引诱报告。有没有办法使用 serve 命令指定发布报告的端口?