我用rxjs
在React Native
. 我打电话Observable.fromPromise(storage.load({key: key})).map(() => value);
显示错误。
我的 rxjs 版本:
"rxjs": "^6.5.3",
"rxjs-compat": "^6.5.3",
我有三个步骤。
第1步:
rxInit().flatMap(() => {
console.log('I can not see the console log');
return rxInit()
}).subscribe(() => {
console.log('I can not see the console log');
// some code...
})
Step2 rxInit():
import { Observable } from 'rxjs';
rxInit() {
console.log('I can see the console log')
return StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
console.log('I can't not see the console log')
if (deviceuuid == null) {
return StorageService.shared.set('deviceuuid', this.deviceuuid);
} else {
return Observable.of(this.deviceuuid);
}
}).do((deviceuuid) => {
// some code...
})
}
关于get()的Step3:
import { Observable } from 'rxjs';
import Storage from 'react-native-storage';
import AsyncStorage from '@react-native-community/async-storage';
let storage = new Storage({
size: 1000,
storageBackend: AsyncStorage,
defaultExpires: null,
})
export default class StorageService {
set(key, value) {
console.log('StorageService set');
return Observable.fromPromise(storage.save({ key: key, data: value })).map(() => value);
}
get(key) {
console.log('It is a ', storage.load({key: key})); // It is a Promise
return Observable.fromPromise(storage.load({key: key})).map(() => value);
}
remove(key) {
return Observable.fromPromise(storage.remove({key: key})).catch(() => Observable.of(null))
}
}
StorageService.shared = new StorageService();
我正在寻找一些答案,有人说是因为rxjs高于6.0,所以我尝试使用
import { from } from 'rxjs';
return from(storage.load({key: key})).map(() => value);
它仍然显示
TypeError: _rxjs.from.fromPromise is not a function
或者
_rxjs.from().map is not a function.
任何帮助,将不胜感激。