我是 Ionic 2 的新手,它是可观察的。我有以下问题。我有一个具有 findAll 功能的数据服务。此函数检查 ifprivate var products != false
是否需要返回该值。如果没有,它需要检查nativestorage
并返回该值,如果它被填充,如果没有,它检查 http get 并返回那个
我的问题,我不想订阅该findAll
函数,以防 http 获取这是可能的,因为它返回一个 observable(如果我是对的),nativestorage getitem 返回一个 promise 而不是 observable。并且已经填充的私有变量的第一个场景也不是可观察的。
希望我足够清楚地描述了这个问题
@Injectable()
export class dataService {
products = false;
constructor (http:Http) {
this.http = http;
}
findAll() {
if (this.products !== false) {
//HOWTO return this.products AS OBSERVABLE?
} else {
//HOWTO check native storage and return observable??
NativeStorage.getItem('products')
//ELSE RETURN observable http get and
//HOWTO ADD TO this.products and NativeStorage.setItem
return this.http.get(productsURL)
.map(res => res.json())
.catch(this.handleError);
}
}
}