为了账号安全,请及时绑定邮箱和手机立即绑定

store.js 调用错误

<template>

<div id="app">

<h1 v-text="title"></h1>

<input v-model="newItem" v-on:keyup.enter="addNew">

<ul>

<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinished(item)">

{{item.label}}

</li>

</ul>

</div>

</template>


<script>

import Store from'./store.js'

export default {

data:function(){

return{

title:'shuiguo title',

items:Store.fetch(),

newItem:''

}

},

watch:{

items:{

handler:function(items){

Store.save()

},

deep:true

}

},

methods:{

toggleFinished:function (item){

item.isFinished=!item.isFinished

},

addNew:function(){

this.items.push(

{

label:this.newItem,

isFinished:true

}),

this.newItem=''

}

}

}

</script>


<style>

#app {

font-family: 'Avenir', Helvetica, Arial, sans-serif;

-webkit-font-smoothing: antialiased;

-moz-osx-font-smoothing: grayscale;

text-align: center;

color: #2c3e50;

margin-top: 60px;

}

.finished{

color:red;

}

</style>

下面是store.js

const STORAGE_KEY="todos-vuejs"

export default{

fetch:function(){

return JSON.parse(

window.localStorage.getItem(

STORAGE_KEY||"[]"

)

)

},

save:function(items){

window.localStorage.setItem(

STORAGE_KEY,JSON.stringify(items)

)

}

}

https://img1.sycdn.imooc.com//5b713ee40001069a13300326.jpg

正在回答

2 回答

fetch:function(){

return JSON.parse(

window.localStorage.getItem(

STORAGE_KEY||"[]"

)

)

},

这里面用“[]”肯定是不对的,不论引用是否正确这里也会报错,不知道是不是这个导致的,

0 回复 有任何疑惑可以回复我~
#1

慕仔4782833

应该用'[]'来代表一个数组,而"[]"就是一个字符串了
2018-08-15 回复 有任何疑惑可以回复我~
#2

qq_雅轩_1 提问者 回复 慕仔4782833

好的,我试试,谢谢
2018-08-15 回复 有任何疑惑可以回复我~
#3

qq_雅轩_1 提问者 回复 慕仔4782833

尝试了一下,问题没有解决
2018-08-15 回复 有任何疑惑可以回复我~
#4

ADDtu 回复 qq_雅轩_1 提问者

return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]' ) window.localStorage.getItem(STORAGE_KEY) 或者 ‘[]’
2018-09-07 回复 有任何疑惑可以回复我~
#5

小潴 回复 qq_雅轩_1 提问者

Store.save()写成 Store.save(items)就行了;不然的话你存的东西为空,所以取值的时候就会报错
2018-12-12 回复 有任何疑惑可以回复我~
查看2条回复

举报

0/150
提交
取消
vue.js入门基础
  • 参与学习       209816    人
  • 解答问题       677    个

本门为vuejs入门教程,详细的讲解加实战,可以帮你进入vuejs的大门

进入课程

store.js 调用错误

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信