vue.js todolist實現代碼
更新時間:2017年10月29日 11:27:06 作者:zjsfdx
這篇文章主要介紹了vue.js todolist實現代碼,需要的朋友可以參考下
案例知識點:
1.vue.js基礎知識
2.HTML5 本地存儲localstorage
store.js代碼
const STORAGE_KEY = 'todos-vue.js'
export default{
fetch(){
return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
},
save(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items));
}
}
App.vue代碼
<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='toogleFinish(item)'>
{{item.label}}
</li>
</ul>
</div>
</template>
<script>
import Store from './store'
export default {
name: 'app',
data () {
return {
title: 'this is a todo list',
items:Store.fetch(),
newItem:''
}
},
watch:{
items:{
handler(items){ //經過變化的數組會作為第一個參數傳入
Store.save(items)
console.log(Store.fetch());
},
deep:true //深度復制
}
},
methods:{
toogleFinish(item){
item.isFinished = !item.isFinished
},
addNew(){
this.items.push({
label:this.newItem,
isFinished:false,
})
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{
text-decoration: underline;
}
</style>

總結
以上所述是小編給大家介紹的vue.js todolist實現代碼,希望對的大家有所幫助!
相關文章
VUE使用vue?create命令創(chuàng)建vue2.0項目的全過程
vue-cli是創(chuàng)建Vue項目的一個腳手架工具,vue-cli提供了vue create等命令,下面這篇文章主要給大家介紹了關于VUE使用vue?create命令創(chuàng)建vue2.0項目的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-07-07
element-ui upload組件上傳文件類型限制問題小結
最近我遇到這樣的問題,接受類型已經加了accept 但是當選擇彈出本地選擇文件時候切換到所有文件 之前的文件類型就本根過濾不掉了,下面小編給大家介紹element-ui upload組件上傳文件類型限制問題小結,感興趣的朋友一起看看吧2024-02-02
使用Vue3和Plotly.js打造一個3D圖在線展示的實現步驟
三維網格圖廣泛應用于科學可視化、醫(yī)學成像、工程設計等領域,用于展示復雜的數據結構和空間分布,本文給大家介紹了使用Vue3和Plotly.js打造一個3D圖在線展示的實現步驟,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-07-07

