首页 >web前端 >js教程 >通过vue + vuex这2样技术实现todolist(详细教程)

通过vue + vuex这2样技术实现todolist(详细教程)

亚连
亚连原创
2018-05-31 16:02:052225浏览

这篇文章主要介绍了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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn