我有一个通过 id 获取帖子的示例 api,在我的组件中,我有一个对象配置要发送到子组件中,这个配置对象包含要在子组件中执行的 api。当 api 在子组件中执行时,我在 httpClient 中收到错误
无法读取未定义的属性“获取”
@Injectable()
export class ApplicationAPI {
constructor(private http: HttpClient) {}
public getPost(id: string): Observable<any> {
return this.http.get<any>("https://jsonplaceholder.typicode.com/posts/" + id);
}
}
父组件
config = {
columns: [
{
defaultValue: { dependFromAPI: this.api.getPost }
}
]
};
子组件
ngOnChanges(changes: SimpleChanges) {
if (changes["config"] && changes["config"].currentValue) {
changes["config"].currentValue.columns[0].defaultValue.dependFromAPI("43").subscribe(item => console.log(item))
}
}