VM296:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()

背景

localStorage做数据持久化时,使用 JSON.parse解析一个字符串,作为持久化的默认值,获取默认值报错:
VM296:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)_第1张图片

解决

const SERVER_ADDRESS = 'server_address'
// const SERVER_ADDRESS_DEF = '192.168.8.60'
const SERVER_ADDRESS_DEF = '"192.168.8.60"'  //注意单引号里面再加个双引号表示字符串

readServerAddress() {
    return JSON.parse(localStorage.getItem(SERVER_ADDRESS) || SERVER_ADDRESS_DEF);
},

你可能感兴趣的:(#,vue,json,字符串,javascript,vue)