最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目

 更新時(shí)間:2017年04月11日 11:50:47   作者:返回主頁 chenxj  
這篇文章主要為大家詳細(xì)介紹了基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用vue.js實(shí)現(xiàn)一個(gè)todolist項(xiàng)目:input輸入框輸入的值會(huì)呈現(xiàn)在下方,并且會(huì)保存在localStorage里面,而且下方的列表點(diǎn)擊之后也會(huì)有變化:

完整代碼:

App.vue

<template>
 <div id="app">
 <h1 v-html = "title"></h1>
 <input v-model="newItem" v-on:keyup.enter="addNew" ></input>
 <ul>
  <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">{{item.label}}</li>
 </ul>
 </div>
</template>

<script>
import Store from './store'
export default {
 data:function(){
 return {
  title:"This Is A Todolist",
  items:Store.fetch(),
  newItem:""
 }
 },
 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>
.finished{
 text-decoration:underline;
}
li{
 list-style:none;
 font-size:1.6em;
 margin-top:10px;
}
#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;
}
input{
 width:230px;
 height:40px;
 border-radius:20px;
 padding: 0.4em 0.35em;
 border:3px solid #CFCFCF;
 font-size: 1.55em;
}
</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))
 }
}
 

詳細(xì)解析

ES6的寫法:

export default {
 name: 'hello',
 data () {
 return {
  msg: 'Welcome to Your Vue.js App'
 }
 }
}

export default 和 export 區(qū)別:

  1).export與export default均可用于導(dǎo)出常量、函數(shù)、文件、模塊等
  2).你可以在其它文件或模塊中通過import+(常量 | 函數(shù) | 文件 | 模塊)名的方式,將其導(dǎo)入,以便能夠?qū)ζ溥M(jìn)行使用
  3).在一個(gè)文件或模塊中,export、import可以有多個(gè),export default僅有一個(gè)
  4).通過export方式導(dǎo)出,在導(dǎo)入時(shí)要加{ },export default則不需要

1.export

//demo1.js
export const str = 'hello world'
export function f(a){ return a+1}
對應(yīng)的導(dǎo)入方式:

//demo2.js
import { str, f } from 'demo1' //也可以分開寫兩次,導(dǎo)入的時(shí)候帶花括號(hào)

2.export default

//demo1.js
export default const str = 'hello world'
對應(yīng)的導(dǎo)入方式:

//demo2.js
import str from 'demo1' //導(dǎo)入的時(shí)候沒有花括號(hào)

當(dāng)最簡單導(dǎo)入的時(shí)候,這個(gè)值是將被認(rèn)為是”入口”導(dǎo)出值。

在App.vue中完成項(xiàng)目編寫:

組件布局將在這里設(shè)置,.vue文件將由vue-loader進(jìn)行加載,.vue內(nèi)同時(shí)包含html、css、js源碼,使組件的獨(dú)立,組件之間可以盡可能地解耦,便于開發(fā)維護(hù)

先看一個(gè)簡單示例:只要isFinished為true就加下劃線,false就不加下劃線:

<template>
 <div id="app">
 <h1 v-html = "title"></h1>
 <ul>
  <li v-for="item in items" v-bind:class="{finished:item.isFinished}">{{item.label}}</li>
 </ul>
 </div>
</template>

<script>
import Hello from './components/Hello'

export default {
 data:function(){
 return {
  title:"this is a todolist",
  items:[
  {
   label:"coding",
   "isFinished":false
  },
  {
   label:"walking",
   "isFinished":true
  }
  ]
 }
 }
}
</script>

<style>
.finished{
 text-decoration:underline;
}
#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;
}
</style>

對于class的控制如上:如果是數(shù)組的話則可以渲染多個(gè)。

再進(jìn)一步完成功能:點(diǎn)擊沒有下劃線的li就會(huì)加下劃線,有下劃線就會(huì)去除下劃線。

需要綁定事件:

復(fù)制代碼 代碼如下:
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">{{item.label}}</li>

還要添加方法toggleFinish():

 methods:{
 toggleFinish:function(item){
  item.isFinished = !item.isFinished
 }
 }

將input輸入的值添加到列表下面

添加input:

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

data對象添加:

newItem:""

添加方法:

//addNew:function(){
// alert(this.newItem)
// this.newItem=""   //添加后加輸入框清空
//}

addNew:function(){
 this.items.push({
 label:this.newItem,
 "isFinished":false 
 })
 this.newItem=""
}

使用localStorage來存儲(chǔ)

使用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))
 }
}

兩個(gè)方法:一個(gè)設(shè)置,一個(gè)獲取

導(dǎo)入:

import Store from './store'

打印一下Store,console.log(Store),可以看到:

由于加入代碼中每次都需要添加還有刪除等等,如果每次都用到store的方法,這就有點(diǎn)麻煩了,所以這里就要用到watch觀察。

 watch:{
 items:{
  handler:function(val,oldVal){
  console.log(val,oldVal)
  },
  deep:true
 }
 },

可以看到打印出:

使用save()方法:

 watch:{
 items:{
  handler:function(items){
  Store.save(items)
  },
  deep:true
 }
 },

一有變化就會(huì)觸發(fā)。

將fetch()方法也加進(jìn)去:

<script>
import Store from './store'
export default {
 data:function(){
 return {
  title:"<span>?</span>this is a todolist",
  items:Store.fetch(),
  newItem:""
 }
 },
 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>


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue+mui實(shí)現(xiàn)圖片的本地緩存示例代碼

    Vue+mui實(shí)現(xiàn)圖片的本地緩存示例代碼

    這篇文章主要介紹了Vue+mui實(shí)現(xiàn)圖片的本地緩存的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • pm2部署vue的實(shí)現(xiàn)步驟

    pm2部署vue的實(shí)現(xiàn)步驟

    本文主要介紹了使用PM2運(yùn)行Vue項(xiàng)目的具體步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Vue infinite update loop的問題解決

    Vue infinite update loop的問題解決

    這篇文章主要介紹了Vue "...infinite update loop..."的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04
  • 淺談VUE項(xiàng)目打包后運(yùn)行頁面一片白問題

    淺談VUE項(xiàng)目打包后運(yùn)行頁面一片白問題

    本文主要介紹了淺談VUE項(xiàng)目打包后運(yùn)行頁面一片白問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2023-01-01
  • 基于Vue3實(shí)現(xiàn)組件封裝的技巧分享

    基于Vue3實(shí)現(xiàn)組件封裝的技巧分享

    這篇文章主要介紹了基于Vue3實(shí)現(xiàn)組件封裝的技巧,本文在Vue3的基礎(chǔ)上針對一些常見UI組件庫組件進(jìn)行二次封裝,旨在追求更好的個(gè)性化,更靈活的拓展,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • Vue監(jiān)聽事件實(shí)現(xiàn)計(jì)數(shù)點(diǎn)擊依次增加的方法

    Vue監(jiān)聽事件實(shí)現(xiàn)計(jì)數(shù)點(diǎn)擊依次增加的方法

    今天小編就為大家分享一篇Vue監(jiān)聽事件實(shí)現(xiàn)計(jì)數(shù)點(diǎn)擊依次增加的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue3使用wangeditor封裝和自定義上傳文件官方教程

    vue3使用wangeditor封裝和自定義上傳文件官方教程

    這篇文章主要為大家介紹了vue3使用wangeditor封裝和自定義上傳文件的官方教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-06-06
  • 詳解Vue.js 響應(yīng)接口

    詳解Vue.js 響應(yīng)接口

    這篇文章主要介紹了Vue.js 響應(yīng)接口的相關(guān)資料,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • element table組件內(nèi)容換行的實(shí)現(xiàn)方案

    element table組件內(nèi)容換行的實(shí)現(xiàn)方案

    這篇文章主要介紹了element table組件內(nèi)容換行的實(shí)現(xiàn)方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Vue首屏白屏問題的原因和解決方法講解

    Vue首屏白屏問題的原因和解決方法講解

    這篇文章主要介紹了Vue首屏白屏問題的原因和解決方法講解,Vue首屏白屏問題是指在頁面初次加載時(shí),部分或全部內(nèi)容無法正常顯示,出現(xiàn)空白的情況。其原因可能是因?yàn)轫撁驿秩舅俣冗^慢,或者是因?yàn)榫W(wǎng)絡(luò)請求等問題導(dǎo)致數(shù)據(jù)無法及時(shí)加載
    2023-05-05

最新評(píng)論

鹤峰县| 鱼台县| 礼泉县| 凯里市| 淄博市| 苏州市| 东海县| 闽侯县| 玉溪市| 磐安县| 宜兰县| 延庆县| 清新县| 古浪县| 赫章县| 当雄县| 米易县| 华安县| 曲麻莱县| 策勒县| 庄浪县| 特克斯县| 固始县| 襄城县| 应用必备| 阜阳市| 泸西县| 天镇县| 临沂市| 叶城县| 江永县| 黄山市| 锦州市| 龙里县| 武汉市| 巨野县| 柞水县| 颍上县| 溆浦县| 临桂县| 左云县|