首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以使用localforage的返回吗?

我可以使用localforage的返回吗?
EN

Stack Overflow用户
提问于 2020-04-29 18:13:39
回答 1查看 269关注 0票数 0

在我的Vue SPA中,我使用localforage来存储承载令牌。

在组件中,我需要令牌来将文件上传到api。

我尝试获取localforage令牌:

代码语言:javascript
复制
            let token = localforage.getItem('authtoken')
            console.log(token)

它可以工作,但结果是一个promise对象:

代码语言:javascript
复制
Promise

result: "eyJ0eXAiOiJKV1QiyuIiwiaWF0IjoxNTg4MTUzMTkzLCJleHAiOjE1ODg…"

status: "resolved"

当我尝试console.log(token.result)时,它返回null

如何访问令牌?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-29 18:23:51

official documentation说明了从存储中读取值的三种不同方法。

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61499344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档