我有fieldObj
件我经常需要的东西.它来自数据库.
下面是我编写的代码,如果localStorage没有,它可以从数据库中获取fieldObj
.
当我试图访问存储在localStorage中的json数据时,它只会显示
[对象]
代码将fieldObj
个本地存储视为字符串.
我需要访问存储在fieldObj
中的键值对.
<script>
export default {
data() {
return {
fields: JSON,
}
},
created() {
if (!localStorage.getItem('field-obj')) {
axios
.get('/api/u/record/', {
withCredentials: true,
})
.then((response) => {
response.data.forEach((el) => {
if (el.COLUMN_TYPE == 'int') el.COLUMN_TYPE = 'number'
})
const fieldObj = response.data[0].fieldObj[0]
this.fields = fieldObj
})
.then(() => {
localStorage.setItem('field-obj', this.fields)
})
} else {
console.log(new Object(localStorage.getItem('field-obj')))
// console.log(localStorage.getItem('field-obj')[0].username)
}
},
}
</script>