vuex的概念
我们在什么情况下会使用Vuex:要频繁的大范围的进行数据共享时一般采用vuex。
在开始学习之前,我们要知道一个vuex应用的核心其实就是store(仓库),它相当于一个容器,包含着应用中大部分的状态。
Vuex 是专门为 Vue.js 设计的状态管理库,以利用 Vue.js 的细粒度数据响应机制来进行高效的状态更新。
一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依旧存储在组件自身的data 中即可。
vuex的特点:
- vuex的状态存储是响应式的,vuex中的状态发生变化,那么相应的组件也会相应地得到高效更新。
- 我们不能直接更改store中的值,只能通过提交(commit)来改变,这样使我们方便的追踪每一个状态的变化。
- 能够在vuex中集中管理共享的数据,易于开发和后期维护
- 能够高效地实现组件之间的数据共享,提高开发效率
vuex的基本使用
1、安装vuex依赖包
npm install vuex --save
2、导入vuex包
import Vuex from 'vuex'
vue.use(Vuex)
3、创建store对象
const store = new Vuex.store({
//state中存放是全局共享的数据
state:{count:0}
})
4、将store对象挂载到Vue实例中
new Vue({
el:'#app',
render:h=>h(app),
router,
//将创建的共享数据对象,挂载到vue实例中
//所有的组件,就可以直接从store中获取全局的数据了
store
})
vuex的核心概念
state
State提供唯一的公共数据源,所有共享的数据都要统一放到Store的State 中进行存储。
//创建store数据源,提供唯一公共数据
const store = new Vuex.store({
state:{count:0}
})
怎么访问state中的全局数据?
方式一:
this.$store.state.全局数据名称
主体中的使用:
<div >
<h3>当前count的值:{{this.$store.state.count}}</h3>
</div>
方式二:
1、从vuex中按需要导入mapState函数
import {mapState} from 'vuex'
2、将全局数据,映射为当前组件的计算属性(通过刚才导入的mapState 函数,将当前组件需要的全局数据,映射为当前组件的computed计算属性)
computed:{
...mapState(['count'])
//...展开运算符,将全局数据count映射为当前组件的计算属性
}
主体中的使用:
<div >
<h3>当前count的值:{{count}}</h3>
</div>
mutation (用于变更store中的数据)
- 只能通过mutation变更Store数据,不可以直接操作Store中的数据。
- 通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。
调用mutation时不带参数
定义 mutation
const: store = new Vuex.Store({
state:{
count:0
}
mutations:{
add(state){
//变更状态
state.count++
}
}
})
触发mutation的第一种方式
methods: {
handle1(){
//触发mutations
this.$store.commit('add')
}
}
调用mutation时带参数
定义mutation
const store = new Vuex.Store({
state:{
count:0
},
mutations:{
addN(state,step){
//变更状态
state.count += step
}
}
})
触发mutation
methods: {
handle1(){
//调用commit函数
//触发mutations 时带参数
this.$store.commit('addN',5)
}
}
触发mutation的第二种方式
1、从vuex中按需导入mapMutations函数
import {mapMutations} from 'vuex'
2、通过刚才导入的mapMutations函数,将需要的mutations函数,映射为当前组件的methods方法
methods:{
...mapMutations(['add','addN']),
//触发mutations
handle1(){
this.add()
},
handle2(){
this.addN(5)
}
}
action
Action 用于处理异步任务。
如果通过异步操作变更数据,必须通过Action,而不能使用Mutation,但是在Action中还是要通过触发Mutation的方式间接变更数据。
(1)触发方式1
触发actions异步任务时不带参数
定义action
const store = new Vuex.Store({
mutations:{
add(state):{
state.count++
}
},
action:{
addAsync(context){
setTimeout(()=>{
//在actions中,不能直接修改state中的数据;
//必须通过context.commit()触发某个mutation 才行
context.commit('add')
},1000)
}
}
})
触发action
methods:{
handle(){
//这里的dispatch函数,专门用来触发action
this.$store.dispatch('addAsync')
}
}
触发actions异步任务时带参数
定义action
const store = new Vuex.Store({
mutations:{
addN(state,step):{
state.count += step
}
},
action:{
addNAsync(context,step){
setTimeout(()=>{
//在actions中,不能直接修改state中的数据;
//必须通过context.commit()触发某个mutation 才行
context.commit('addN',step)
},1000)
}
}
})
触发action
methods:{
handle(){
//这里的dispatch函数,专门用来触发action
this.$store.dispatch('addNAsync',5)
}
}
(2)触发方式2
1、从vuex中按需导入mapActions函数
import {mapActions} from 'vuex'
2、通过刚才导入的mapActions函数,将需要的actions 函数,映射为当前组件的methods方法
methods:{
...mapActions(['addAsync','addNAsync']),
btnHandler1(){
this.addAsync()
},
btnHandler2(){
this.addNAsync(5)
},
}
getter
不会修改store中的数据,只有一个包装数据的作用
- Getter 用于对 Store 中的数据进行加工处理形成新的数据。
- Getter可以对Store中已有的数据加工处理之后形成新的数据,类似Vue的计算属性。
- Store中数据发生变化,Getter的数据也会跟着变化。
定义getter
const store = new Vuex.Store({
state:{
count:0
},
getters:{
showNum(state){
return'当前最新的数量是【'+ state.count +'】'
}
}
})
(1)、使用方式1
this.$store.getters.名称
<h3>{{this.$store.getters.showNum}}</h3>
(2)、使用方式2
import { mapGetters } from 'vuex'
computed:{
...mapGetters(['showNum'])
}
<h3>{{showNum}}</h3>
以上就是关于vuex的基本知识点的全部学习,只有多加练习才能使用得更好,才能防止记忆的遗忘。加油吧,程序员们。