인프런 커뮤니티 질문&답변
localStorage 관련 문의드립니다.
작성
·
240
1
안녕하세요!
TodoList.vue 에서 localStorage JSON.parse으로 변경 시
[Vue warn]: Error in created hook: "SyntaxError: Unexpected token 바 in JSON at position 0"
found in
---> <TodoList> at src/components/TodoList.vue
<App> at src/App.vue
<Root>
라는 오류가 뜨고 리스트가 나오질 않습니다
코드를 아무리 봐도 문제가 뭔지 모르겠네요...어디가 잘못된걸까요 ㅜㅜ
TodoList.vue 코드 첨부해드립니다 ..
<template>
<section>
<ul>
<li v-for="(todoItem, index) in todoItems" class="shadow" v-bind:key="todoItem">
<i class="checkBtn fas fa-check" v-on:click="toggleComplete"></i>
{{todoItem}}
<span class="removeBtn" v-on:click="removeTodo(todoItem, index)">
<i class="removeBtn fas fa-trash-alt"></i>
</span>
</li>
</ul>
</section>
</template>
<script>
export default {
data: function() {
return {
todoItems: []
}
},
methods: {
removeTodo: function(todoItem, index) {
this.todoItems.splice(index, 1); // 화면에서 지우기 인덱스 하나를 지우자(하나를 콕 집어서 splice)
localStorage.removeItem(todoItem); //데이터베이스에서 지우기
},
toggleComplete: function(){
}
},
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
0
캡틴판교
지식공유자
안녕하세요 토마토님 제가 답변이 조금 늦었네요! :)
위에 created 쪽에서 로컬 스토리지의 데이터를 불러오는 구간에 JSON.parse를 사용하셔서 그런 것 같습니다. 저장하실 때 객체 형태로 저장하셨나요? :)





