首頁 > web前端 > js教程 > 主體

透過vue + vuex這2樣技術實現todolist(詳細教學)

亚连
發布: 2018-05-31 16:02:05
原創
2213 人瀏覽過

這篇文章主要介紹了vue vuex todolist的實作範例程式碼,我覺得還挺不錯的,現在分享給大家,也給大家做個參考。

todolist demo

最近有空重新看了一下vuex,然後又寫了一個todolist小demo,原理比較簡單,主要是自己規範了一下程式碼的寫法.

下載位址:vue-test_jb51.rar

效果圖

根元件

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

<script>
import { createNamespacedHelpers } from &#39;vuex&#39;
import TypeFilter from &#39;./filter&#39;
import AddTodo from &#39;./addTodo&#39;
import TodoItem from &#39;./item&#39;

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

<style lang=&#39;scss&#39; scoped>
@import &#39;./style.scss&#39;;
</style>
登入後複製

#過濾條件元件

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

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

<style lang=&#39;scss&#39; scoped>
@import &#39;./style.scss&#39;;
</style>
登入後複製

新增待辦事項元件

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

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

<style lang=&#39;scss&#39; scoped>
@import &#39;./style.scss&#39;;
</style>
登入後複製

單一待辦事項元件

##

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

<script>
export default {
 name: &#39;todo-items&#39;,
 props: [&#39;data&#39;, &#39;filter&#39;, &#39;index&#39;, &#39;handleRemove&#39;, &#39;handleToggle&#39;],
 computed: {
 show() {
 return (
 this.filter === &#39;ALL&#39; ||
 (this.filter === &#39;UNDO&#39; && !this.data.status) ||
 (this.filter === &#39;DONE&#39; && 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=&#39;scss&#39; scoped>
@import &#39;./style.scss&#39;;
</style>
登入後複製

vuex部分(模組)


const state = {
 list: [],
 types: [&#39;ALL&#39;, &#39;UNDO&#39;, &#39;DONE&#39;],
 filter: &#39;ALL&#39;
}
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
}
登入後複製

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

jQuery實作的回車觸發按鈕事件功能範例

jQuery Cookie實作切換皮膚功能

Angular 元件之間的互動的範例程式碼

以上是透過vue + vuex這2樣技術實現todolist(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板