欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue + vuex todolist的實現(xiàn)示例代碼

 更新時間:2018年03月09日 09:31:42   作者:darcrand  
這篇文章主要介紹了vue + vuex todolist的實現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

todolist demo

最近有空重新看了一下vuex,然后又寫了一個todolist小demo,原理比較簡單,主要是自己規(guī)范了一下代碼的寫法.

下載地址 :vue-test_jb51.rar

效果圖

根組件

<template>
 <div class='container'>
 <h1 class='title'>todo list demo</h1>
 <type-filter
 :types='types'
 :filter='filter'
 :handleUpdateFilter='handleUpdateFilter'
 />
 <add-todo :handleAdd='handleAdd' />
 <todo-item
 v-for='(item,index) in list'
 :key='item.id'
 :index='index'
 :data='item'
 :filter='filter'
 :handleRemove='handleRemove'
 :handleToggle='handleToggle'
 />
 </div>
</template>

<script>
import { createNamespacedHelpers } from 'vuex'
import TypeFilter from './filter'
import AddTodo from './addTodo'
import TodoItem from './item'

const { mapState, mapMutations } = createNamespacedHelpers('TodoList')
export default {
 name: 'todo-list-demo',
 components: { TypeFilter, TodoItem, AddTodo },
 computed: {
 ...mapState(['list', 'types', 'filter'])
 },
 methods: {
 ...mapMutations([
 'handleAdd',
 'handleRemove',
 'handleToggle',
 'handleUpdateFilter'
 ])
 }
}
</script>

<style lang='scss' scoped>
@import './style.scss';
</style>

過濾條件組件

<template>
 <ul class='types'>
 <li
 v-for='(item,index) in types'
 :key='index + item'
 :class='filterClass(item)'
 @click='handleUpdateFilter(item)'
 >{{item}}</li>
 </ul>
</template>

<script>
export default {
 name: 'type-filter',
 props: ['types', 'filter', 'handleUpdateFilter'],
 methods: {
 filterClass(filter) {
 return { filter: true, active: filter === this.filter }
 }
 }
}
</script>

<style lang='scss' scoped>
@import './style.scss';
</style>

添加待辦組件

<template>
 <input
 type='text'
 name='add-todo'
 id='add-todo-input'
 class='add-todo'
 @keyup.enter='add'
 placeholder='input then hit enter'
 />
</template>

<script>
export default {
 name: 'add-todo',
 props: ['handleAdd'],
 methods: {
 add(e) {
 const val = e.target.value.trim()
 if (val) {
 this.handleAdd({
  id: new Date().getTime(),
  message: val,
  status: false
 })
 e.target.value = ''
 }
 }
 }
}
</script>

<style lang='scss' scoped>
@import './style.scss';
</style>

單個待辦事項組件

<template>
 <p v-if='show' class='todo-item'>
 <span
 :class='messageClass(data.status)'
 @click='handleToggle(data.id)'
 >{{index+1}}. {{data.message}}<i class='date'>{{dateFormat(data.id)}}</i></span>
 <span
 class='delete'
 @click='handleRemove(data.id)'
 >Delete</span>
 </p>
</template>

<script>
export default {
 name: 'todo-items',
 props: ['data', 'filter', 'index', 'handleRemove', 'handleToggle'],
 computed: {
 show() {
 return (
 this.filter === 'ALL' ||
 (this.filter === 'UNDO' && !this.data.status) ||
 (this.filter === 'DONE' && this.data.status)
 )
 }
 },
 methods: {
 dateFormat(time) {
 const date = new Date(time)
 return `(${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()})`
 },
 messageClass: status => ({ message: true, done: status })
 }
}
</script>

<style lang='scss' scoped>
@import './style.scss';
</style>

vuex部分(模塊)

const state = {
 list: [],
 types: ['ALL', 'UNDO', 'DONE'],
 filter: 'ALL'
}
const mutations = {
 handleAdd(state, item) {
 state.list = [...state.list, item]
 },
 handleRemove(state, id) {
 state.list = state.list.filter(obj => obj.id !== id)
 },
 handleToggle(state, id) {
 state.list = state.list.map(
 obj => (obj.id === id ? { ...obj, status: !obj.status } : obj)
 )
 },
 handleUpdateFilter(state, filter) {
 state.filter = filter
 }
}
export default {
 namespaced: true,
 state,
 mutations
}

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

您可能感興趣的文章:

相關(guān)文章

  • vue預(yù)覽 pdf、word、xls、ppt、txt文件的實現(xiàn)方法

    vue預(yù)覽 pdf、word、xls、ppt、txt文件的實現(xiàn)方法

    這篇文章主要介紹了vue預(yù)覽 pdf、word、xls、ppt、txt文件的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 微信小程序用戶盒子、宮格列表的實現(xiàn)

    微信小程序用戶盒子、宮格列表的實現(xiàn)

    這篇文章主要介紹了微信小程序用戶盒子、宮格列表,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vue3如何使用setup代替created

    vue3如何使用setup代替created

    Vue3中的setup是一個新的生命周期函數(shù),它可以用來代替組件中的 data和一些生命周期函數(shù)(如created和beforeMount),這篇文章主要介紹了vue3如何使用setup代替created的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • vue實現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能示例

    vue實現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能示例

    這篇文章主要介紹了vue實現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能,結(jié)合實例形式分析了基于vue.js的數(shù)據(jù)庫操作及頁面圖片顯示相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Vue3使用時應(yīng)避免的10個錯誤總結(jié)

    Vue3使用時應(yīng)避免的10個錯誤總結(jié)

    Vue?3已經(jīng)穩(wěn)定了相當(dāng)長一段時間了。許多代碼庫都在生產(chǎn)環(huán)境中使用它,其他人最終都將不得不遷移到Vue?3。我現(xiàn)在有機會使用它并記錄了我的錯誤,下面這些錯誤你可能想要避免
    2023-03-03
  • vue中常用的縮寫方式

    vue中常用的縮寫方式

    這篇文章主要介紹了vue中常用的縮寫方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue如何在CSS中使用data定義的數(shù)據(jù)淺析

    Vue如何在CSS中使用data定義的數(shù)據(jù)淺析

    這篇文章主要給大家介紹了關(guān)于Vue如何在CSS中使用data定義的數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-05-05
  • html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總

    html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總

    這篇文章主要介紹了html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11
  • vue-cli 首屏加載優(yōu)化問題

    vue-cli 首屏加載優(yōu)化問題

    這篇文章主要介紹了vue-cli 首屏加載優(yōu)化問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue 使用v-model實現(xiàn)控制子組件顯隱效果

    Vue 使用v-model實現(xiàn)控制子組件顯隱效果

    v-model 可以實現(xiàn)雙向綁定的效果,允許父組件控制子組件的顯示/隱藏,同時允許子組件自己控制自身的顯示/隱藏,本文給大介紹Vue 使用v-model實現(xiàn)控制子組件顯隱,感興趣的朋友一起看看吧
    2023-11-11

最新評論