export const getLocal = (key, type) => {
let index = localStorage.getItem(key)
console.log(index)
let item = JSON.parse(index)
if (String(item) === 'null') {
item = type
}
return item
}
undefined
vue-router.mjs?f169:3451
SyntaxError: "undefined" is not valid JSON
at JSON.parse ()
at getLocal (storage.js?9327:21:1)
at state (user.js?29c6:13:1)
at setup (pinia.mjs?b318:1196:1)
at eval (pinia.mjs?b318:1436:1)
at EffectScope.run (reactivity.esm-bundler.js?89dc:34:1)
at eval (pinia.mjs?b318:1436:1)
at EffectScope.run (reactivity.esm-bundler.js?89dc:34:1)
at createSetupStore (pinia.mjs?b318:1434:1)
at createOptionsStore (pinia.mjs?b318:1224:1)
SyntaxError: "undefined" is not valid JSON
这报错很明显了啊
JSON.parse这个方法无法将其转化非JSON.stringify类型的数据。
你这个index没有值 可写成 let item = index ? JSON.parse(index) : ''
undefined 你看你那里输出得这个,还有你肯定那个地方没处理导致这个样子
export const getLocal = (key, type) => {
let index = localStorage.getItem(key);
if(!index) return type; //添加这一行代码
console.log(index)
let item = JSON.parse(index); //index是undefined ,这里是问题所在!
if (String(item) === 'null') return type;
return item
}