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


Posted in Javascript onNovember 30, 2016

一、介绍Vuex

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化,适合于构建中大型单页应用。

1、什么是状态管理模式?
看个简单的例子:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width">
 <title>Vuex Demo 01</title>
<script src="http://cdn.bootcss.com/vue/1.0.26/vue.min.js"></script>
<script src="http://cdn.bootcss.com/vuex/0.8.2/vuex.min.js"></script>
</head>
<body>
 <!-- 2、view,映射到视图的数据counterValue; -->
 <h3>Count is {{ counterValue }}</h3>
 <div>
 <button @click="increment">Increment +1</button>
 <button @click="decrement">Decrement -1</button>
 </div>
</body>
<script>
var app = new Vue({
 el: 'body',
 store: new Vuex.Store({
 // 1、state,驱动应用的数据源;
 state: {
 count: 0
 },
 mutations: {
 INCREMENT: function(state, amount) {
 state.count = state.count + amount
 },
 DECREMENT: function(state, amount) {
 state.count = state.count - amount
 }
 }
 }),
 vuex: {
 getters: {
 counterValue: function(state) {
 return state.count
 }
 },
 // 3、actions,响应在view上的用户输入导致的状态变化。
 actions: {
 increment: function({ dispatch, state }){
 dispatch('INCREMENT', 1)
 },
 decrement: function({ dispatch, state }){
 dispatch('DECREMENT', 1)
 }
 }
 }
})
</script>
</html>

代码中标识了:

1、state,驱动应用的数据源;
2、view,映射到视图的数据counterValue;
3、actions,响应在view上的用户输入导致的状态变化。

用简单示意图表示他们之间的关系:

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

我们知道,中大型的应用一般会遇到多个组件共享同一状态的情况:

1、多个视图依赖于同一状态
2、来自不同视图的行为需要变更同一状态

于是需要把组件的共享状态抽取出来,以一个全局单例模式管理,另外,需要定义和隔离状态管理中的各种概念并强制遵守一定的规则。

这就是 Vuex 背后的基本思想,借鉴了 Flux、Redux、和 The Elm Architecture。与其他模式不同的是,Vuex 是专门为 Vue.js 设计的状态管理库,以利用 Vue.js 的细粒度数据响应机制来进行高效的状态更新。

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

2、Vuex的核心概念

1)、State: 单一状态树,用一个对象包含了全部的应用层级状态,作为一个『唯一数据源(SSOT)』而存在,每个应用将仅仅包含一个 store 实例。
2)、Getters: Vuex 允许我们在 store 中定义『getters』(可以认为是 store 的计算属性)。
3)、Mutations: Vuex 中的 mutations 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数。
4)、Actions: 类似于 mutation,不同在于:①Action 提交的是 mutation,而不是直接变更状态;②Action 可以包含任意异步操作。
5)、Modules: 为解决单一状态树导致应用的所有状态集中在一个store对象的臃肿问题,Vuex将store分割到模块(module)。每个模块拥有自己的 state、mutation、action、getters、甚至是嵌套子模块——从上至下进行类似的分割。
接着我们开始构建备忘录应用,在以下构建过程的介绍中,再加深理解上述概念。

二、环境安装

1.安装 vue-cli

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

2.初始化应用

vue init webpack vue-notes-demo
cd vue-notes-demo
npm install // 安装依赖包
npm run dev // 启动服务

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

结果为:

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

目录结构为:

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

三、功能模块

先看下我们要做的demo的效果为:

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

主要功能模块为:

新增计划,新增一个计划,编辑区显示空的计划内容。

移除计划,删除一个计划之后,计划列表少了该计划。

所有计划的总时长,将所有的计划时间加起来。

四、项目组件划分

在原来的目录结构的调整下,最终的目录结构为:

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

下面详细介绍下:

1、组件部分
1).首页组件:Home.vue

<template>
 <div class="jumbotron">
 <h1>我的备忘录</h1>
 <p>
 <strong>
 <router-link to="/time-entries">创建一个计划</router-link>
 </strong>
 </p>
 </div>
</template>

2).计算计划总时长组件:Sidebar.vue

<template>
 <div class="panel panel-default">
 <div class="panel-heading">
 <h3 class="text-center">所有计划的总时长: {{ time }} 小时</h3>
 </div>

 </div>
</template>

<script>
 export default {
 computed: {
 time() {
 return this.$store.state.totalTime
 }
 }
 }
</script>

3).计划列表组件:TimeEntries.vue

<template>
 <div>
 <router-link
 v-if="$route.path !== '/time-entries/log-time'"
 to="/time-entries/log-time"
 class="btn create-plan">
 创建
 </router-link>

 <div v-if="$route.path === '/time-entries/log-time'">
 <h3>新的计划</h3>
 </div>

 <hr>

 <router-view></router-view>

 <div class="time-entries">
 <p v-if="!plans.length"><strong>还没有任何计划(┬_┬),快去创建吧?(●-`Д´-)ノ</strong></p>

 <div class="list-group">
 <a class="list-group-item" v-for="(plan,index) in plans">
 <div class="row">
 <div class="col-sm-2 user-details">
 <img :src="plan.avatar" class="avatar img-circle img-responsive" />
 <p class="text-center">
 <strong>
  {{ plan.name }}
 </strong>
 </p>
 </div>

 <div class="col-sm-2 text-center time-block">
 <p class="list-group-item-text total-time">
 <span class="glyphicon glyphicon-time">计划总时间:</span>
 {{ plan.totalTime }}
 </p>
 <p class="label label-primary text-center">
 <span class="glyphicon glyphicon-calendar">开始时间:</span>
 {{ plan.date }}
 </p>
 </div>

 <div class="col-sm-7 comment-section">
 <p>备注信息:{{ plan.comment }}</p>
 </div>
 <button
 class="btn btn-xs delete-button"
 @click="deletePlan(index)">
 X
 </button>
 </div>
 </a>

 </div>
 </div>
 </div>
</template>
<script>
 export default {
 name : 'TimeEntries',
 computed : {
 plans () {
 return this.$store.state.list
 }
 },
 methods : {
 deletePlan(idx) {
 // 减去总时间
 this.$store.dispatch('decTotalTime',this.plans[idx].totalTime)
 // 删除该计划
 this.$store.dispatch('deletePlan',idx)
 }
 }
 }
</script>

4).新增计划组件:LogTime.vue

<template>
 <div class="form-horizontal">
 <div class="form-group">
 <div class="col-sm-6">
 <label>开始日期:</label>
 <input
 type="date"
 class="form-control"
 v-model="date"
 placeholder="Date"
 />
 </div>
 <div class="col-sm-6">
 <label>总时间 :</label>
 <input
 type="number"
 class="form-control"
 v-model="totalTime"
 placeholder="Hours"
 />
 </div>
 </div>
 <div class="form-group">
 <div class="col-sm-12">
 <label>备注  :</label>
 <input
 type="text"
 class="form-control"
 v-model="comment"
 placeholder="Comment"
 />
 </div>
 </div>
 <button class="btn btn-primary" @click="save()">保存</button>
 <router-link to="/time-entries" class="btn btn-danger">取消</router-link>
 <hr>
 </div>
</template>

<script>
 export default {
 name : 'LogTime',
 data() {
 return {
 date : '',
 totalTime : '',
 comment : ''
 }
 },
 methods:{
 save() {
 const plan = {
 name : 'eraser',
 image : 'https://pic.cnblogs.com/avatar/504457/20161108225210.png',
 date : this.date,
 totalTime : this.totalTime,
 comment : this.comment
 };
 this.$store.dispatch('savePlan', plan)
 this.$store.dispatch('addTotalTime', this.totalTime)
 this.$router.go(-1)
 }
 }
 }
</script>

2、vuex中用来存储数据的划分为:
1).初始化vuex.Store: index.js

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'

Vue.use(Vuex);

const state = {
 totalTime: 0,
 list: []
};

export default new Vuex.Store({
 state,
 mutations,
 actions
})

State: 单一状态树,用一个state对象包含了全部的应用层级状态,代码中只new 了一次store实例 Vuex.Store。

2).负责触发事件和传入参数:actions.js

import * as types from './mutation-types'

export default {
 addTotalTime({ commit }, time) {
 commit(types.ADD_TOTAL_TIME, time)
 },
 decTotalTime({ commit }, time) {
 commit(types.DEC_TOTAL_TIME, time)
 },
 savePlan({ commit }, plan) {
 commit(types.SAVE_PLAN, plan);
 },
 deletePlan({ commit }, plan) {
 commit(types.DELETE_PLAN, plan)
 }
};

实践中,我们会经常会用到 ES2015 的 参数解构 来简化代码(特别是我们需要调用 commit 很多次的时候):

actions: {
 increment ({ commit }) {
 commit('increment')
 }
}

3).注册各种数据变化的方法: mutations.js

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 id="app">
 <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 id="wrapper">
 <nav class="navbar navbar-default">
 <div class="container">
 <a class="navbar-brand" href="#">
 <i class="glyphicon glyphicon-time"></i>
 备忘录
 </a>
 <ul class="nav navbar-nav">
 <li><router-link to="/home">首页</router-link></li>
 <li><router-link to="/time-entries">计划列表</router-link></li>
 </ul>
 </div>
 </nav>
 <div class="container">

 <div class="col-sm-9">
 <router-view></router-view>
 </div>
 <div class="col-sm-3">
 <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实践】

参考:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
科讯商业版中用到的ajax空间与分页函数
Sep 02 Javascript
jquery监控数据是否变化(修正版)
Apr 12 Javascript
Jquery submit()无法提交问题
Apr 21 Javascript
javascript实现的一个带下拉框功能的文本框
May 08 Javascript
最全的Javascript编码规范(推荐)
Jun 22 Javascript
Bootstrap Scrollspy源码学习
Mar 02 Javascript
node+vue实现用户注册和头像上传的实例代码
Jul 20 Javascript
基于Node.js模板引擎教程-jade速学与实战1
Sep 17 Javascript
原生JS实现日历组件的示例代码
Sep 22 Javascript
微信小程序的生命周期的详解
Oct 19 Javascript
JavaScript获取用户所在城市及地理位置
Apr 21 Javascript
js实现直播点击飘心效果
Aug 19 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
Nov 30 #Javascript
JavaScript实现拖拽元素对齐到网格(每次移动固定距离)
Nov 30 #Javascript
jquery.Callbacks的实现详解
Nov 30 #Javascript
javascript中活灵活现的Array对象详解
Nov 30 #Javascript
如何处理JSON中的特殊字符
Nov 30 #Javascript
Angular.JS判断复选框checkbox是否选中并实时显示
Nov 30 #Javascript
Node.js开发教程之基于OnceIO框架实现文件上传和验证功能
Nov 30 #Javascript
You might like
PHP超级全局变量数组小结
2012/10/04 PHP
php采集文章中的图片获取替换到本地(实现代码)
2013/07/08 PHP
PHP imagegrabscreen和imagegrabwindow(截取网站缩略图)的实例代码
2013/11/07 PHP
PHP配置把错误日志以邮件方式发送方法(Windows系统)
2015/06/23 PHP
PHP实现JS中escape与unescape的方法
2016/07/11 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
2019/08/14 PHP
JQuery Tips(2) 关于$()包装集你不知道的
2009/12/14 Javascript
ajax上传时参数提交不更新等相关问题
2012/12/11 Javascript
文本有关的样式和jQuery求对象的高宽问题分别说明
2013/08/30 Javascript
js实现上传图片预览的方法
2015/02/09 Javascript
jQuery实现根据生日计算年龄 星座 生肖
2016/11/23 Javascript
canvas绘制七巧板
2017/02/03 Javascript
原生JS实现ajax与ajax的跨域请求实例
2017/12/01 Javascript
vue-cli3使用 DllPlugin 实现预编译提升构建速度
2019/04/24 Javascript
详解vue中$nextTick和$forceUpdate的用法
2019/12/11 Javascript
深入浅析vue全局环境变量和模式
2020/04/28 Javascript
[01:10:58]Spirit vs NB Supermajor小组赛 A组败者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
[04:22]DOTA2大事件之护国神翼
2020/08/14 DOTA
PyMongo安装使用笔记
2015/04/27 Python
举例详解Python中循环语句的嵌套使用
2015/05/14 Python
Python中splitlines()方法的使用简介
2015/05/20 Python
详解Django中Request对象的相关用法
2015/07/17 Python
对python:print打印时加u的含义详解
2018/12/15 Python
Python tkinter 下拉日历控件代码
2020/03/04 Python
jupyter notebook 调用环境中的Keras或者pytorch教程
2020/04/14 Python
浅谈Python3多线程之间的执行顺序问题
2020/05/02 Python
python实现简易版学生成绩管理系统
2020/06/22 Python
Python爬虫设置ip代理过程解析
2020/07/20 Python
Html5与App的通讯方式详解
2019/10/24 HTML / CSS
Charlotte Tilbury美国官网:英国美妆品牌
2017/10/13 全球购物
高中数学教师求职信
2013/10/30 职场文书
招商经理岗位职责
2013/11/16 职场文书
2015年采购工作总结
2015/04/10 职场文书
机关干部正风肃纪心得体会
2016/01/15 职场文书
nginx配置proxy_pass中url末尾带/与不带/的区别详解
2021/03/31 Servers
使用golang编写一个并发工作队列
2021/05/08 Golang