Uncaught (in promise) SyntaxError: Unexpected token u in JSON at position 0

1,294 阅读1分钟

在vue3的项目练习中遇到的bug,网上搜了一圈,终于找到一个靠谱的解释,并顺利解决了问题。 在此记录一下

bug背景

在chrome浏览器中测试正常,在360极速浏览器中报错 Uncaught (in promise) SyntaxError: Unexpected token u in JSON at position 0

	const todoStorage = {
		fetch() {
			let todos = JSON.parse(localStorage.getItem("vue3-todos") || "[]");
			todos.forEach((todo, index) => {
				todo.id = index + 1;
			});
			return todos;
		},
		save(todos) {      
			localStorage.setItem("vue3-todos", JSON.stringify(todos));
		},
	};

  export function useTodos(state){
    const todos = ref(todoStorage.fetch()); //改成todoStorage.fetch 错误消失
    }

错误原因详细的解释

segmentfault.com/a/119000001…