Vuex是专门为Vue.js开发的状态管理模式
- 当多个组件同时需要访问一个变量时,组件之间互相传递过于麻烦,就可以用Vuex来把这些变量来集中起来
- 如登陆的状态、个人信息、地理位置、收藏夹购物车等等
核心概念
- State:数据及单一状态树
- Getters:类似组件的计算属性computed
- Mutations:存放方法
- Actions做异步操作
- Modules:划分模块数据保存
![](https://img2020.cnblogs.com/blog/1978202/202004/1978202-20200401154843128-432778448.png)
用法和vue-router差不多
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state:{
counter:1000
},
mutations:{},
actions:{},
getters:{},
modules:{}
})
export default store
<h2>{{$store.state.counter}}</h2>
<button @click="$store.state.counter++">+</button>
<button @click="$store.state.counter--">-</button>
- 但是官方并不推荐这样修改,如果不通过mutations修改state,数据无法追踪,最好能通过mutations(方法)
const store = new Vuex.Store({
state:{
counter:1000
},
mutations:{
increment(state){
state.counter++
},
decrement(state){
state.counter--
}
}
}
<button @click="add">+</button>
<button @click="minus">-</button>
methods:{
add(){
this.$store.commit('increment')
},
minus(){
this.$store.commit('decrement')
}
}
getters的一些操作
getters:{
powerCounter(state){
return state.counter * state.counter
},
get23stu(state){
return state.students.filter(stu=> stu.age >22)
},
get23stuLength(state,getters){
return getters.get23stu.length
},
getAgeStu(state){
return age=>{
return state.students.filter(s=>s.age===age)
}
}
}
Mutations
- Vuex的store状态更新的唯一方式:提交mutations
mutations:{
increment(state){
state.counter++
},
decrement(state){
state.counter--
},
incrementCount(state,count){
state.counter += count
},
addStu(state,stu){
state.students.push(stu)
}
}
<h2>年龄大于22的人数共有{{$store.getters.get23stuLength}}</h2>
<h2>getters传参数,年龄等于22的人</h2>
<h2>{{$store.getters.getAgeStu(22)}}</h2>
addCount(count){
this.$store.commit('incrementCount',count)
},
addStu(){
const stu = {id:123,name:'test',age:35}
this.$store.commit('addStu',stu)
}
<h2>---修改info的字段---</h2>
<h2>{{$store.state.info}}</h2>
<button @click="updateInfo">添加</button>
updateInfo(){
this.$store.commit('updateInfo')
}
updateInfo(state){
Vue.set(state.info,'address','HangZhou-ZheJiang')
}
Vue.delete(state.info,'age')
在写mutations的时候,会发现很多相似的名字很容易写错,此时可以定义一个常量,创建一个文件来保存这些常量
export const INCREMENT = 'increment'
- 在app.vue和 index.js里修改对应代码
import {
INCREMENT
} from './store/mutations-type'
add(){
this.$store.commit(INCREMENT)
},
mutations:{
[INCREMENT](state){
state.counter++
},
}
Action 做异步操作
- 当进行异步操作时,mutations无法进行响应监听
- 举个例子,做一个定时器,里面修改info里的属性的值,用actions来做
- app.vue
updateInfo(){
this.$store.dispatch('aUpdateInfo')
}
actions:{
aUpdateInfo(context){
setTimeout(() => {
context.commit('updateInfo')
}, 1000);
}
}
updateInfo(state){
state.info.name = '异步操作actions'
}
modules
getCount(state,getters,rootState){
return state.name+rootState.counter
}
高级用法
mapGetters,mapActions的使用
import { mapActions } from 'vuex'
methods:{
...mapActions(['addCart']),
this.addCart(product).then(res=>{
console.log(res);
})
}
- //传actions,用了mapActions后
- 上面直接调用映射的addCart时,vuex内部就会做dispatch这个操作
mapGetters,mapACtions的2种写法
...mapActions:{
add:'addCart'
}
...mapActions(['addCart'])
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee