【发布时间】:2019-05-07 21:47:55
【问题描述】:
我尝试使用 Ionic Storage 模块来存储一些值,例如我的身份验证令牌:
/**
* Get Token
*/
public get token(): string {
this.storage.get(this.LS_TOKEN).then((val) => {
console.log(val);
this._token.next(val);
console.log( this._token.getValue());
});
return this._token.getValue();
// return 'testtttt';
}
我尝试了多种东西,直接返回值,设置值并返回变量......
但我总是得到一个null,奇怪的是,如果我直接返回一个字符串,它会起作用,当我console.log val 它显示我想要的字符串,但返回总是空的。 .
我做错了什么?
编辑:
作为对第一个答案的回应,我尝试了这个:
/**
* Get Token
*/
public get token() {
this.tokenPromise().then(yourToken => {
console.log(yourToken);
return yourToken;
});
}
public tokenPromise() {
return new Promise((resolve, reject) => {
this.storage.get(this.LS_TOKEN).then((val) => {
resolve(val);
}).catch(ex => {
reject(ex);
});
});
}
我的问题是一样的,在我尝试使用的组件中:console.log(this.sharedService.token);
它仍然是空的
【问题讨论】:
-
请显示您的 storage.set 方法。