Vuex2.0+Vue2.0构建备忘录应用实践(3)

import * as types from './mutation-types' export default { // 增加总时间 [types.ADD_TOTAL_TIME] (state, time) { state.totalTime = state.totalTime + time }, // 减少总时间 [types.DEC_TOTAL_TIME] (state, time) { state.totalTime = state.totalTime - time }, // 新增计划 [types.SAVE_PLAN] (state, plan) { // 设置默认值,未来我们可以做登入直接读取昵称和头像 const avatar = 'https://pic.cnblogs.com/avatar/504457/20161108225210.png'; state.list.push( Object.assign({ name: 'eraser', avatar: avatar }, plan) ) }, // 删除某计划 [types.DELETE_PLAN] (state, idx) { state.list.splice(idx, 1); } };

使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个 app 包含的 mutation 一目了然:

// mutation-types.js export const SOME_MUTATION = 'SOME_MUTATION' mutations: { // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名 [SOME_MUTATION] (state) { // mutate state } }

4).记录所有的事件名: mutation-types.js

// 增加总时间或者减少总时间 export const ADD_TOTAL_TIME = 'ADD_TOTAL_TIME'; export const DEC_TOTAL_TIME = 'DEC_TOTAL_TIME'; // 新增和删除一条计划 export const SAVE_PLAN = 'SAVE_PLAN'; export const DELETE_PLAN = 'DELETE_PLAN';

配合上面常量替代 mutation 事件类型的使用

3、初始化部分
入口文件渲染的模版index.html比较简单:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue-notes-demo</title> </head> <body> <div> <router-view></router-view> </div> </body> </html>

入口文件main.js的代码:

import Vue from 'vue'; import App from './App'; import Home from './components/Home'; import TimeEntries from './components/TimeEntries.vue' import VueRouter from 'vue-router'; import VueResource from 'vue-resource'; import store from './vuex/index'; // 路由模块和HTTP模块 Vue.use(VueResource); Vue.use(VueRouter); const routes = [ { path: '/home', component: Home }, { path : '/time-entries', component : TimeEntries, children : [{ path : 'log-time', // 懒加载 component : resolve => require(['./components/LogTime.vue'],resolve), }] }, { path: '*', component: Home } ] const router = new VueRouter({ routes // short for routes: routes }); // router.start(App, '#app'); const app = new Vue({ router, store, ...App, }).$mount('#app');

代码中 ...App 相当于 render:h => h(App)
初始化组件App.vue为:

<!-- // src/App.vue --> <template> <div> <nav> <div> <a href="#"> <i></i> 备忘录 </a> <ul> <li><router-link to="/home">首页</router-link></li> <li><router-link to="/time-entries">计划列表</router-link></li> </ul> </div> </nav> <div> <div> <router-view></router-view> </div> <div> <sidebar></sidebar> </div> </div> </div> </template> <script> import Sidebar from './components/Sidebar.vue' export default { components: { 'sidebar': Sidebar }, } </script> <style> .router-link-active { color: red; } body { margin: 0px; } .navbar { height: 60px; line-height: 60px; background: #333; } .navbar a { text-decoration: none; } .navbar-brand { display: inline-block; margin-right: 20px; width: 100px; text-align: center; font-size: 28px; text-shadow: 0px 0px 0px #000; color: #fff; padding-left: 30px; } .avatar { height: 75px; margin: 0 auto; margin-top: 10px; /* margin-bottom: 10px; */ } .text-center { margin-top: 0px; /* margin-bottom: 25px; */ } .time-block { /* padding: 10px; */ margin-top: 25px; } .comment-section { /* padding: 20px; */ /* padding-bottom: 15px; */ } .col-sm-9 { float: right; /* margin-right: 60px; */ width: 700px; min-height: 200px; background: #ffcccc; padding: 60px; } .create-plan { font-size: 26px; color: #fff; text-decoration: none; display: inline-block; width: 100px; text-align: center; height: 40px; line-height: 40px; background: #99cc99; } .col-sm-6 { margin-top: 10px; margin-bottom: 10px; } .col-sm-12 { margin-bottom: 10px; } .btn-primary { width: 80px; text-align: center; height: 30px; line-height: 30px; background: #99cc99; border-radius: 4px; border: none; color: #fff; float: left; margin-right: 10px; font-size: 14px; } .btn-danger { display: inline-block; font-size: 14px; width: 80px; text-align: center; height: 30px; line-height: 30px; background: red; border-radius: 4px; text-decoration: none; color: #fff; margin-bottom: 6px; } .row { padding-bottom: 20px; border-bottom: 1px solid #333; position: relative; background: #f5f5f5; padding: 10px; /* padding-bottom: 0px; */ } .delete-button { position: absolute; top: 10px; right: 10px; } .panel-default { position: absolute; top: 140px; right: 60px; } </style>

至此,实践结束,一些原理性的东西我还需要多去理解^_^

源代码:【vuex2.0实践

参考:

vuex2.0文档

关于Vue.js 2.0的Vuex 2.0 你需要更新的知识库

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxzfp.html