inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Vue.js 중급 강좌 - 웹앱 제작으로 배워보는 Vue.js, ES6, Vuex

TodoList 컴포넌트의 할 일 완료 기능 구현

JSON 에러가 나는데 도와주세요

1084

jarchive

작성한 질문수 5

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>

es6 vuex vuejs javascript

답변 1

1

캡틴판교

안녕하세요 brocoli님, created 라이프 사이클 훅에서 JSON.parse() 하는 코드를 이렇게 바꿔서 디버깅 해보시겠어요?

var item = localStorage.getItem(localStorage.key(i))

debugger;

this.todoItems.push(JSON.parse(item));

0

jarchive

캡틴판교님 감사합니다!!!

Chrome 개발자 모드 확장이 안됨

0

302

1

깃 권한 요청드립니다

0

128

1

vue.js 중급 리포지토리 권한 관련

0

130

1

vuex + axios 질문 있습니다!

1

210

2

깃 권한 요청드립니다!

0

168

1

강의 깃주소 문의

0

151

1

router-view에 props를 어떻게 넘길 수 있나요?

1

292

2

Vue가 인식되지 않는 현상

0

213

1

기초강좌는 어디있나요?

1

200

2

App.vue가 필요한 이유

0

199

1

getter가 정의되어 있지 않아 오류가 발생합니다.

1

264

1

뷰 라이프사이클

1

194

1

TSLint 말고 TSLint Vue 설치해도 되나요?

1

379

3

로컬 스토리지는 어디에 있나요?

1

290

1

vuex 실행시 새로고침해야지만 리스트에 나타나는 현상

1

403

2

export default 관련한 질문

0

369

2

깃허브 vue-todo 접근불가에 따른 확인요청

1

363

2

깃허브에 문제있는것 같습니다.

1

283

2

인프런 강의 재생 화면 구성 변경 문의드립니다

1

314

2

addTodo Helper 함수 적용

1

252

1

vuex 헬퍼 전역 설정

1

254

2

github 권한요청드립니다.

1

262

2

이벤트 위치에 대한 궁금증 입니다.

1

229

2

구조 차이에 대한 문의

1

351

2