Javascript 值始终从本地存储返回null(Ionic 3+;5)

Javascript 值始终从本地存储返回null(Ionic 3+;5),javascript,angular,ionic3,local-storage,storage,Javascript,Angular,Ionic3,Local Storage,Storage,我刚刚在我的ionic 3应用程序中添加了存储插件(带有angular 5)。 文档非常简单。。。至少我是这么想的 由于某些原因,我可以完美地存储该值(在调试工具中也可见),但我无法将其读回 我的代码: import {Injectable} from '@angular/core'; import {Storage} from '@ionic/storage'; @Injectable() export class LocalStorageService { constructor(priv

我刚刚在我的ionic 3应用程序中添加了存储插件(带有angular 5)。 文档非常简单。。。至少我是这么想的

由于某些原因,我可以完美地存储该值(在调试工具中也可见),但我无法将其读回

我的代码:

import {Injectable} from '@angular/core';
import {Storage} from '@ionic/storage';

@Injectable()
export class LocalStorageService {
constructor(private storage: Storage) {}

setItem(key: string, value: string | Object | number): Promise<void> {
    return this.storage.set(key, JSON.stringify(value));
}

getItem(key: string): Promise<string> {
    return this.storage.get(key)
        .then((value: any) => {
            console.log('---------------------------------------');
            console.log(value);
            return value;
        });
}

removeItem(key: string): Promise<void> {
    return this.storage.remove(key);
}

clear(): Promise<void> {
    return this.storage.clear();
}
}
所以我不知道我做错了什么。。。? 也许是旁注,因为我不知道它是否相关。 我已经使用了爱奥尼亚公司的SQLite插件,效果很好。所以现在我尝试添加localstorage来存储简单的数据对

提前谢谢你的帮助


善良的问候

getItem(键:string):Promise
返回一个Promise,但您已经在方法中对其调用了
.then()
return
.then()
中不起作用,因为它是同步方法中的异步方法。要么在不使用
.then()
的情况下返回承诺,要么在该方法中不返回任何内容并使用
.then()
执行操作。

控制台的
日志(值)
getItem()
下为空??每次都是。。。当我打开chrome调试工具时,在应用程序->本地存储下,我可以看到具有正确值的键…在您的组件中,您是否在调用
setItem()
之前调用
getItem()
?不,这是我第一次想到的,编写还没有完成。但我甚至在重新启动应用程序时尝试了它(因此变量在上一次运行时已在本地存储中准备就绪…)。尽管如此,这个值仍然是空的。我不相信这是真的,你总是可以从
then()
返回,通过再次执行
then()
来利用返回值,即链接它们,例如,在这里,他会这样做:
this.getItem(auth)。然后((tokenValue)=>{…})
通常代码只是一行,返回this.storage.get(key),我把它放进去检查值。。。因为没有“then()”,该值仍然为空。。。
NgModule({
declarations: [
    ...
],
imports: [
    ...
    IonicStorageModule.forRoot(),
    ...
],
exports: [
    ...
]
export class GlobalModule {
}