【发布时间】:2020-01-13 14:50:03
【问题描述】:
在 Ionic 4 Angular 8 项目中,我尝试在调用 API 之前获取存储中的信息。
这是我的代码
public getData(id) {
return this.storage.get(id).then(
content => {
if (content == null) {
return this.getObject(id).subscribe(
result => {
return result;
}
);
} else {
return content;
}
}
);
}
如何调用函数“getData”?
myService.getData(id).then
这个返回字符串或者observable我应该订阅,这两种情况如何处理?
【问题讨论】:
-
使用 RxJS 中的
of运算符将您的字符串放入 Observable 中,以标准化您的方法调用。此外,你应该在你的方法上加上一个返回类型并使用 RxJS Observable 而不是 promise。