网上看到的vue-cli环境下双向绑定的方法亲测好用,可以拿来拓展

本文介绍了一个使用Vue.js构建的任务管理器应用,包括组件、状态管理和本地存储的实现。通过v-model和v-on指令,实现了任务的动态显示与更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1..vue文件

<template>

<div id="dataList">

<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="toggleFinish(item)">

{{item.label}}

</li>

</ul>

</div>

</template>

stript部分:

<script>

import Store from '../store'

export default{

data(){

return{

title: "每 日 任 务",

items: Store.fetch(),

/*

[

{"label":"俯卧撑10个","isFinished":true}

{"label":"背单词10个","isFinished":false}

 

]

*/

newItem:''

//liclass:"thisisliclass" //赋值class名称

}

},

watch:{

items:{

handler:function(items){

Store.save(items)

},

deep:true

}

},

methods:{

toggleFinish:function(item){

item.isFinished = !item.isFinished

},

addNew:function(){

this.items.push({

label: this.newItem,

isFinished:false

})

this.newItem=''

}

}

}

</script>

 

<style>

#app{width:400px;margin:0 auto;}

.finished{text-decoration:line-through;}

</style>

3.store.js文件

const STORAGE_KEY = 'todos-vuejs'

export default{

fetch(){

return JSON.parse(window.localStorage.getItem(

STORAGE_KEY) || '[]')

},

save (items){

window.localStorage.setItem(

STORAGE_KEY,JSON.stringify(items))

}

}

方法借助于toggleFinish事件与localstorage存值实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值