1、Unexpected end of JSON input原因及如何解决
总是遇到一个报错,导致有时候渲染有问题,我们看看啥报错:
11:29:07.092 [Vue warn]: Error in render: "SyntaxError: Unexpected end of JSON input"
11:29:07.131 (found at pages/account/account.vue:1)
11:29:07.152 SyntaxError: Unexpected end of JSON input
初看是JSON解析的问题。但是account代码很简单,没什么json解析问题啊,后来发现是取userInfo信息时报错
getters: {
userInfo(state) {
if (!state.userInfo) {
state.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
}
return state.userInfo
}
},
如果userInfo不存在时,去storage里面取,如果不存在,这时候就为空。JSON.parse(''),所以就报这个错。
JSON.parse('')
// VM483:1 Uncaught SyntaxError: Unexpected end of JSON input
// at Object.parse (<anonymous>)
// at <anonymous>:1:6
那么知道了原因,稍微改一下即可:
getters: {
userInfo(state) {
if (!state.userInfo) {
let _user = uni.getStorageSync('userInfo')
state.userInfo = _user ? JSON.parse(_user) : null
}
return state.userInfo
}
},