这次给大家带来vue.js之vuex(状态管理),使用vue.js的vuex(状态管理)注意事项有哪些,下面就是实战案例,一起来看一下。
vuex是一个状态管理工具,类似于redux.
安装vuex
npm install vuex --save
vuex 的状态管理存储是响应式的:就是当你的组件使用到了 vuex 的某个状态,一旦它发生改变了,所有关联的组件都会自动更新相对应的数据。
不能直接修改 vuex 的状态:修改 vuex 的状态唯一途径是提交(commit) mutations 来实现修改
如上图,vuex为vue components建立起了一个完整的生态圈,包括开发中的api调用一环。围绕这个生态圈,简要介绍一下各模块在核心流程中的主要功能:
vue components:vue组件。html页面上,负责接收用户操作等交互行为,执行dispatch方法触发对应action进行回应。
dispatch:操作行为触发方法,是唯一能执行action的方法。
actions:操作行为处理模块。负责处理vue components接收到的所有交互行为。包含同步/异步操作,支持多个同名方法,按照注册的顺序依次触发。向后台api请求的操作就在这个模块中进行,包括触发其他action以及提交mutation的操作。该模块提供了promise的封装,以支持action的链式触发。
commit:状态改变提交操作方法。对mutation进行提交,是唯一能执行mutation的方法。
mutations:状态改变操作方法。是vuex修改state的唯一推荐方法,其他修改方式在严格模式下将会报错。该方法只能进行同步操作,且方法名只能全局唯一。操作之中会有一些hook暴露出来,以进行state的监控等。
state:页面状态管理容器对象。集中存储vue components中data对象的零散数据,全局唯一,以进行统一的状态管理。页面显示所需的数据从该对象中进行读取,利用vue的细粒度数据响应机制来进行高效的状态更新。
getters:state对象读取方法。图中没有单独列出该模块,应该被包含在了render中,vue components通过该方法读取全局state对象。
在main.js文件中代码如下
import vue from 'vue'import app from './app.vue'import 'jquery'import vrouter from 'vue-router'//导入vueximport vuex from 'vuex'import apple from './components/apple.vue'import banana from './components/banana.vue'// 全局使用路由vue.use(vrouter)// 设置全局vue.use(vuex)// 实例化vuexlet store = new vuex.store({ state: { totalprice: 0
}, getters: {
gettotal (state) { return state.totalprice
}
}, mutations: {
increment (state, price) {
state.totalprice += price
},
decrement (state, price) {
state.totalprice -= price
}
}, // actions是在mutations之前的动作,只能调用mutations,不能调用state
// 其实actions也可以理解为中介
// actions 和 mutations的区别:
// actions: 是异步的操作,再去触发mutations
// mutations: 是同步的操作
actions: {
increase (context, price) {
context.commit('increment', price)
}
}
})// 实例化routerlet router = new vrouter({
......
})/* eslint-disable no-new */new vue({ el: '#app',
router,
store,//设置全局
template: '<app/>', components: { app }
})
在apple.vue中代码如下:
<template>
<div class="hello">
<h1>{{msg}}</h1>
<button @click="addone">add one</button>
<button @click="minusone">minus one</button>
</div></template><script>
export default {
data () { return { msg: 'i am apple', price: 5
}
}, methods: {
addone () { //使用了vuex的actions
this.$store.dispatch('increase', this.price)
},
minusone () { //未使用vuex的actions
this.$store.commit('decrement', this.price)
}
}
}</script>
在banana.vue中代码如下:
<template>
<div class="hello">
<h1>{{msg}}</h1>
<button @click="addone">add one</button>
<button @click="minusone">minus one</button>
</div></template><script>
export default {
data () { return { msg: 'i am banana', price: 15
}
}, methods: {
addone () { //未使用vuex的actions
this.$store.commit('increment', this.price)
},
minusone () { //未使用vuex的actions
this.$store.commit('decrement', this.price)
}
}
}</script>
在显示界面 app.vue文件中
<template>
<div id="app">

{{ totalprice }} <apple></apple>
<banana></banana>
</div></template><script>
import apple from './components/apple.vue'
import banana from './components/banana.vue'
export default { components: {
apple,
banana
}, //计算属性
computed: {
totalprice () {// return this.$store.state.totalprice
return this.$store.getters.gettotal
}
}
}</script>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue.js路由的其他操作
vue.js的路由参数
以上就是vue.js之vuex(状态管理)的详细内容。