VueX 的学习
VueX
解决了多个父子组件之间进行状态改变以及数据传递的问题,通过使用Vuex将多个组件频繁使用的值放入一个公共的 “容器” 中,间接的简化了组件间状态管理的问题。在具有VueX
的Vue项目中,我们只需要把这些值定义在VueX中,就可以在整个Vue项目的组件中使用。
在项目中安装vuex
npm i vuex -s
index.js
中的内容
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
//创建VueX对象
const store = new Vuex.Store({
state:{
//管理的值
name:'hello world'
}
})
export default store
main.js中引入vuex对象
main.js相关代码
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
store, //store:store 和router一样,直接写变量名即可
render: h => h(App)
})
使用Vuex
在不同的组件中使用时,直接从 “容器” 中取出即可,使用插值表达式取值
<template>
<div id='app'>
name:
<h1>{{ $store.state.name }}</h1>
</div>
</template>
在组件方法中取出对应的值
...,
methods:{
add(){
console.log(this.$store.state.name)
}
},
...
不要在此处更改state
中的状态的值
VueX的工作过程
在VueX对象中,除了state
对象,还有用来操作state
中数据的方法集,以及当我们需要对state
中的数据需要加工的方法集等等成员。
成员列表:
- state 存放状态
- mutations state成员操作
- getters 加工state成员给外界
- actions 异步操作
- modules 模块化状态管理
VueX的工作流程
在工作时,Vue
组件如果调用某个VueX
的方法过程中需要向后端请求时或者说出现异步操作时,需要dispatch
VueX中actions
的方法,以保证数据的同步。可以说,action
的存在就是为了让mutations
中的方法能在异步操作中起作用。
如果没有异步操作,那么我们就可以直接在组件内提交状态中的Mutations
中自己编写的方法来达成对state
成员的操作。注意,1.3.3节
中有提到,不建议在组件中直接对state
中的成员进行操作,这是因为直接修改(例如:this.$store.state.name = 'hello'
)的话不能被VueDevtools
所监控到。
最后被修改后的state成员会被渲染到组件的原位置当中去。
Mutations
mutations
是操作state
数据的方法的集合,比如对该数据的修改、增加、删除等等。
mutations
方法都有默认的形参:
([state] [,payload])
state
是当前VueX
对象中的state
payload
是该方法在被调用时传递参数使用的
例如,我们编写一个方法,当被执行时,能把下例中的name值修改为"lkj"
,我们只需要这样做
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.store({
state:{
name:'helloVueX'
},
mutations:{
edit(state){
state.name = 'lkj'
}
}
})
export default store // 输出该模块
---------------------------------------------------------------------------------
// 在组件中,我们只需要调用这个`mutation`
this.$store.commit('edit')
// Mutation传单个值
this.$store.commit('edit',15)
// 当需要多参提交时,推荐把他们放在一个对象中来提交:
this.$store.commit('edit',{age:15,sex:'男'})
// 另一种提交方式
this.$store.commit({
type:'edit',
payload:{
age:15,
sex:'男'
}
})
增删state中的成员
为了配合Vue的响应式数据,我们在Mutations的方法中,应当使用Vue提供的方法来进行操作。如果使用delete
或者xx.xx = xx
的形式去删或增,则Vue不能对数据进行实时响应。
-
Vue.set 为某个对象设置成员的值,若不存在则新增
例如对state对象中添加一个age成员
Vue.set(state,"age",15)
-
Vue.delete 删除成员
将刚刚添加的age成员删除
Vue.delete(state,'age')
2.3 Getters
可以对state中的成员加工后传递给外界
Getters中的方法有两个默认参数
- state 当前VueX对象中的状态对象
- getters 当前getters对象,用于将getters下的其他getter拿来用
getters:{
nameInfo(state){
return "姓名:"+state.name
},
fullInfo(state,getters){
return getters.nameInfo+'年龄:'+state.age
}
}
// 组件中调用
this.$store.getters.fullInfo
Actions
直接在mutation
方法中进行异步操作,将会引起数据失效。可以使用Actions来专门进行异步操作,最终提交mutation
方法。
Actions
中的方法有两个默认参数
context
上下文(相当于箭头函数中的this)对象payload
挂载参数
例如,我们在两秒中后执行edit
方法
由于setTimeout
是异步操作,所以需要使用actions
actions:{
aEdit(context,payload){
setTimeout(()=>{
context.commit('edit',payload)
},2000)
}
}
// 在组件中调用:
this.$store.dispatch('aEdit',{age:15})
Models模块
当项目庞大,状态非常多时,可以采用模块化管理模式。Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter
、甚至是嵌套子模块——从上至下进行同样方式的分割。
models:{
a:{
state:{},
getters:{},
....
}
}
// 组件内调用模块a的状态:
this.$store.state.a
// 而提交或者dispatch某个方法和以前一样,会自动执行所有模块内的对应类型的方法:
this.$store.commit('editKey')
this.$store.dispatch('aEditKey')
Models模块的细节
-
模块中
mutations
和getters
中的方法接受的第一个参数是自身局部模块内部的state
models:{ a:{ state:{key:5}, mutations:{ editKey(state){ state.key = 9 } }, .... } }
-
getters
中方法的第三个参数是根节点状态models:{ a:{ state:{key:5}, getters:{ getKeyCount(state,getter,rootState){ return rootState.key + state.key } }, .... } }
-
actions
中方法获取局部模块状态是context.state
,根节点状态是context.rootState
models:{ a:{ state:{key:5}, actions:{ aEidtKey(context){ if(context.state.key === context.rootState.key){ context.commit('editKey') } } }, .... } }
参考文档: