-
카테고리
-
세부 분야
프론트엔드
-
해결 여부
미해결
JSON 에러가 나는데 도와주세요
21.10.23 21:44 작성 조회수 500
1
안녕하세요.
TodoList.vue에서 localStorage에서 키 값을 기준으로 아이템을 꺼내오는 부분이 있습니다.
여기서 JSON.parse() 를 하는데요, 개발자도구에서 확인해보니 이쪽에서 에러가 납니다.
에러 내용은 아래와 같이 TodoList에서 납니다.
[SyntaxError: Unexpected token e in JSON at position 1]
vue.runtime.esm.js?2b0e:1897 SyntaxError: Unexpected token e in JSON at position 1
at JSON.parse (<anonymous>)
at VueComponent.created (TodoList.vue?cb67:33)
at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1863)
at callHook (vue.runtime.esm.js?2b0e:4235)
at VueComponent.Vue._init (vue.runtime.esm.js?2b0e:5022)
at new VueComponent (vue.runtime.esm.js?2b0e:5168)
at createComponentInstanceForVnode (vue.runtime.esm.js?2b0e:3304)
at init (vue.runtime.esm.js?2b0e:3133)
at createComponent (vue.runtime.esm.js?2b0e:6022)
at createElm (vue.runtime.esm.js?2b0e:5969)
JSON포맷 맞추느라고 TodoInput.vue에서 값 넣을 때
var obj = {"completed": false, "item": this.newTodoItem}
이런식으로 해도 소용이 없엇습니다.
구글링으로는 해결을 하지 못해서 문의드립니다.
아래는 저의 TodoList.vue 입니다.
<template>
<div>
<ul>
<li v-for="(todoItem, index) in todoItems" v-bind:key="todoItem">
{{ todoItem }}
<span class="removeBtn" v-on:click="removeTodo(todoItem, index)">
<i class="fas fa-trash-alt"></i>
</span>
</li>
</ul>
</div>
</template>
<script>
export default {
data: function(){
return {
todoItems: []
}
},
methods: {
removeTodo: function(todoItem, index){
console.log(todoItem, index);
localStorage.removeItem(todoItem);
this.todoItems.splice(index, 1);
}
},
created: function(){
if (localStorage.length > 0) {
for(var i = 0; i< localStorage.length; i++){
if (localStorage.key(i) !== 'loglevel:webpack-dev-server') {
this.todoItems.push(JSON.parse(localStorage.getItem(localStorage.key(i))));
}
}
}
}
}
</script>
<style scoped>
ul {
list-style-type: none;
padding-left: 0px;
margin-top: 0;
text-align: left;
}
li {
display: flex;
min-height: 50px;
height: 50px;
line-height: 50px;
margin: 0.5rem 0;
padding: 0 0.9rem;
background: white;
border-radius: 5px;
}
.checkBtn {
line-height: 45px;
color: #62acde;
margin-right: 5px;
}
.checkBtnCompleted {
color: #b3adad;
}
.textCompleted {
text-decoration: line-through;
color: #b3adad;
}
.removeBtn {
margin-left: auto;
color: #de4343;
}
</style>
답변을 작성해보세요.
1
장기효(캡틴판교)
지식공유자21.10.25 22:51
안녕하세요 brocoli님, created 라이프 사이클 훅에서 JSON.parse() 하는 코드를 이렇게 바꿔서 디버깅 해보시겠어요?
var item = localStorage.getItem(localStorage.key(i))
debugger;
this.todoItems.push(JSON.parse(item));
답변 1