[英]Console.log display object but save in localstorage is [object Object]
I use function parseJwt to convert token, in console it display object, but when i save it in localstorage it display [object Object], and i cant use JSON.parse it in another component我使用函数 parseJwt 来转换令牌,在控制台中它显示对象,但是当我将它保存在 localstorage 中时它显示 [object Object],我不能在另一个组件中使用 JSON.parse 它
localStorage.setItem('response', parseJwt(JSON.stringify(response.data.token)));
console.log(parseJwt(JSON.stringify(response.data.token)));
Local storage only supports storing string
.本地存储只支持存储string
。 So when you try storing an object like that, it won't work as expected.因此,当您尝试存储这样的对象时,它不会按预期工作。 All you need to do, is stringify the object first:您需要做的就是首先对对象进行字符串化:
localStorage.setItem('response', JSON.stringify(parseJwt(JSON.stringify(response.data.token))));
And the reason why you're seeing [object Object], is that [object Object] is the default string representation of a JavaScript Object.您看到 [object Object] 的原因是 [object Object] 是 JavaScript 对象的默认字符串表示形式。 You can read more about here https://stackoverflow.com/questions/4750225/what-does-object-object-mean#:~:text=%5Bobject%20Object%5D%20is%20the%20default%20string%20representation%20of%20a%20JavaScript,alert(o)%3B%20%2F%2F%20foo你可以在这里阅读更多关于https://stackoverflow.com/questions/4750225/what-does-object-object-mean#:~:text=%5Bobject%20Object%5D%20is%20the%20default%20string%20representation% 20of%20a%20JavaScript,alert(o)%3B%20%2F%2F%20foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.