博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue part5.2 vuex todolist 改写
阅读量:6243 次
发布时间:2019-06-22

本文共 4317 字,大约阅读时间需要 14 分钟。

代码

main部分

index.html

      
vue_demo
View Code

src/base.css

body {  background: #fff;}.btn {  display: inline-block;  padding: 4px 12px;  margin-bottom: 0;  font-size: 14px;  line-height: 20px;  text-align: center;  vertical-align: middle;  cursor: pointer;  box-shadow: inset 0 1px 0 rgba(225, 225, 225, 0.2), 0 1px 2px rgba(0, 0, 0,0.15);  border-radius: 4px;}.btn-danger {  color: #fff;  background-color: #da4f49;  border: 1px solid #bd362f;}.btn-danger:hover {  color: #fff;  background-color: #bd362f;}.btn:focus {  outline: none;}
View Code

src/main.js

/** * Created by infaa on 2018/9/19. */import Vue from 'vue'import App from './components/App'import store from './store'import './base.css'/* eslint-disable no-new */new Vue({  el: '#app',  render: h => h(App),  store})
View Code

 

工具部分

src/util/storageUtil.js

/** * Created by infaa on 2018/9/20. */const TODO_KEY = 'todos_key'export default {  saveTodos (value) {    window.localStorage.setItem(TODO_KEY, JSON.stringify(value))  },  readTodos () {    return JSON.parse(window.localStorage.getItem(TODO_KEY) || '[]')  }}
View Code

 

store 部分  (vuex)

src/store/action.js

/** * Created by infaa on 2018/9/22. */import {ADD_TODO, DELETE_TODO, SELECT_ALL_TODOS, CLEAR_ALL_COMPLETED, RECEIVE_TODOS} from './mutation-types'import storeageUtil from '../util/storageUtil'export default {  addTodo ({commit}, todo) {    commit(ADD_TODO, {todo})  },  deleteTodo ({commit}, index) {    commit(DELETE_TODO, {index})  },  selectAllTodos ({commit}, check) {    commit(SELECT_ALL_TODOS, {check})  },  clearALLCompleted ({commit}) {    commit(CLEAR_ALL_COMPLETED)  },  reqTodos ({commit}) {    // 模拟异步获取延迟1秒    setTimeout(() => {      const todos = storeageUtil.readTodos()      commit(RECEIVE_TODOS, todos)    }, 1000)  }}
View Code

src/store/getters.js

/** * Created by infaa on 2018/9/22. */export default {  totalCount (state) {    return state.todos.length  },  completeCount (state) {    return state.todos.reduce((preTotal, todo) => {      return preTotal + (todo.complete ? 1 : 0)    }, 0)  },  isAllSelected (state, getters) {    return getters.totalCount === getters.completeCount && state.todos.length > 0  }}
View Code

src/store/index.js

/** * Created by infaa on 2018/9/22. */import Vue from 'vue'import Vuex from 'vuex'import state from './state'import mutations from './mutations'import actions from './actions'import getters from './getters'Vue.use(Vuex)/* eslint-disable new-cap */export default new Vuex.Store({  state,  mutations,  actions,  getters})
View Code

src/store/mutation-types.js

/** * Created by infaa on 2018/9/22. * mutation 名称常量 */export const ADD_TODO = 'add_todo'export const DELETE_TODO = 'delete_todo'export const SELECT_ALL_TODOS = 'select_all_todos'export const CLEAR_ALL_COMPLETED = 'clear_all_completed'export const RECEIVE_TODOS = 'receive_todos' // 接收todos
View Code

src/store/mutation.js

/** * Created by infaa on 2018/9/22. */import {ADD_TODO, DELETE_TODO, SELECT_ALL_TODOS, CLEAR_ALL_COMPLETED, RECEIVE_TODOS} from './mutation-types'export default {  [ADD_TODO] (state, {todo}) {    state.todos.unshift(todo)  },  [DELETE_TODO] (state, {index}) {    state.todos.splice(index, 1)  },  [SELECT_ALL_TODOS] (state, {check}) {    state.todos.forEach(todo => (todo.complete = check))  },  [CLEAR_ALL_COMPLETED] (state) {    state.todos = state.todos.filter(todo => (!todo.complete))  },  [RECEIVE_TODOS] (state, todos) {    state.todos = todos  }}
View Code

src/store/state.js

/** * Created by infaa on 2018/9/22. */export default {  todos: []}
View Code

 

组件部分

components/App.vue

View Code

components/TodoFooter.vue

View Code

components/TodoHeader.vue

View Code

components/TodoItem.vue

View Code

components/TodoList.vue

View Code

 

转载于:https://www.cnblogs.com/infaaf/p/9691630.html

你可能感兴趣的文章
Prolog 逻辑推导语言
查看>>
又搬回来了233
查看>>
CentOS7下单机部署RabbltMQ环境的操作记录
查看>>
C# 编码命名规则
查看>>
centos7执行 wget命令: command not found的两种解决方法
查看>>
Win8Metro(C#)数字图像处理--2.25二值图像距离变换
查看>>
包管理和环境管理软件Anaconda
查看>>
使用curator 来管理elasticsearch的index
查看>>
manjaro折腾手记
查看>>
vue - webpack.dev.conf.js for merge
查看>>
Jvm(16),jvm创建对象---对象在内存中的创建
查看>>
Microsoft SQL Server 2005 Service fails to start
查看>>
【048】HTML 笔记
查看>>
RDA 编译器的搭建
查看>>
sqlserver重命名字段名称
查看>>
学习面试题Day07
查看>>
HttpServletRequest/HttpServletResponse乱码问题解决
查看>>
yum源配置
查看>>
Python操作redis
查看>>
spring+springmvc+mybatis+maven整合
查看>>