在我的Vue SPA中,我使用localforage来存储承载令牌。
在组件中,我需要令牌来将文件上传到api。
我尝试获取localforage令牌:
let token = localforage.getItem('authtoken')
console.log(token)它可以工作,但结果是一个promise对象:
Promise
result: "eyJ0eXAiOiJKV1QiyuIiwiaWF0IjoxNTg4MTUzMTkzLCJleHAiOjE1ODg…"
status: "resolved"当我尝试console.log(token.result)时,它返回null
如何访问令牌?
发布于 2020-04-29 18:23:51
official documentation说明了从存储中读取值的三种不同方法。
localforage.getItem('authtoken').then(function(value) {
// This code runs once the value has been loaded
// from the offline store.
console.log(value);
}).catch(function(err) {
// This code runs if there were any errors
console.log(err);
});
// Callback version:
localforage.getItem('authtoken', function(err, value) {
// Run this code once the value has been
// loaded from the offline store.
console.log(value);
});
// async/await
try {
const value = await localforage.getItem('authtoken');
// This code runs once the value has been loaded
// from the offline store.
console.log(value);
} catch (err) {
// This code runs if there were any errors.
console.log(err);
}https://stackoverflow.com/questions/61499344
复制相似问题