Angular 如何正确地在角度服务中实现这一点?
我为Woocommerce找到了一个API库,并试图将其集成到Angular中 对于获取类别,似乎需要这一行Angular 如何正确地在角度服务中实现这一点?,angular,typescript,rxjs,angular6,Angular,Typescript,Rxjs,Angular6,我为Woocommerce找到了一个API库,并试图将其集成到Angular中 对于获取类别,似乎需要这一行 const json = await WooWorker.getCategories(); 我的服务如下所示: import { WooWorker } from "api-ecommerce"; GetAllUsers(): Observable<any> { return this.http.get(`${BASEURL}/categories`);
const json = await WooWorker.getCategories();
我的服务如下所示:
import { WooWorker } from "api-ecommerce";
GetAllUsers(): Observable<any> {
return this.http.get(`${BASEURL}/categories`);
}
从“api电子商务”导入{WooWorker};
GetAllUsers():可观察{
返回这个.http.get(`${BASEURL}/categories`);
}
我需要替换此部分返回此.http.get(
${BASEURL}/categories)代码>
使用此const json=await woowerner.getCategories()代码>因为API使用第二种方法。如何正确使用observable?GetAllUsers():observable{
GetAllUsers(): Observable<any> {
return WooWorker.getCategories();
}
返回WooWorker.getCategories();
}
按照上面给出的方法尝试,从中返回数据您可以用这种方式写入数据
import { WooWorker } from "api-ecommerce";
import { Observable, of } from 'rxjs';
async GetAllUsers(): Observable<any> {
const json = await WooWorker.getCategories();
return of(json);
}
从“api电子商务”导入{WooWorker};
从'rxjs'导入{可观察的};
异步GetAllUsers():可观察{
const json=await woowerner.getCategories();
返回(json);
}
of
是一个RxJS运算符,它将输入转换为可观察的。感谢您的回复。它能与这种API一起工作吗?我问这个问题是因为API提到了react native,但是react导入的文件看起来像简单的JS。如果它返回json,那么你需要调用你的观察者函数来获取json谢谢你的回复。它会与github.com/inspireui/api-ecommerce/tree/master/src这种api一起工作吗?我这样问是因为API提到了react native,但react导入的文件看起来像简单的JS应该有用。因为您必须通过angular项目中的某个NPM包安装它。所以不调用angular的http模块不会导致任何问题?不,不会导致任何问题。先试试看,看起来你想试试。使用(WooWorker.getCategories())中的或延迟(()=>WooWorker.getCategories())
。