https://www.cnblogs.com/samve/p/10726629.html
vue:vuex中mapState拱雏、mapGetters、mapActions輔助函數(shù)及Module的使用
vue提供了注入機制,就是把我們的store 對象注入到根實例中。vue的根實例就是 new
Vue構(gòu)造函數(shù)儒拂,然后在所有的子組件中this.$store 來指向store 對象寸潦。在index.js 中,我們用export
store把store已經(jīng)暴露出去了社痛,然后直接在main.js中引入store并注入store即可见转。
// The Vue build version to load with the `import` command// (runtime-only or standalone) has been set in webpack.base.conf with an alias.importVuefrom'vue'importElementUIfrom'element-ui'import'element-ui/lib/theme-chalk/index.css'importAppfrom'./App'importrouterfrom'./router/router.js'importstorefrom'./store'importechartsfrom'echarts'
一、普通store中使用mapState蒜哀、mapGetters輔助函數(shù):
在src目錄下建立store文件夾:
?
index.js如下:
importVuefrom'vue';importVuexfrom'vuex';Vue.use(Vuex);conststate={//要設置的全局訪問的state對象showFooter:true,changableNum:0count:0//要設置的初始屬性值};constgetters = {//實時監(jiān)聽state值的變化(最新狀態(tài))isShow(state) {//方法名隨意,主要是來承載變化的showFooter的值returnstate.showFooter? },? getChangedNum(){//方法名隨意,主要是用來承載變化的changableNum的值returnstate.changebleNum? }};constmutations = {? show(state) {//自定義改變state初始值的方法斩箫,這里面的參數(shù)除了state之外還可以再傳額外的參數(shù)(變量或?qū)ο?;state.showFooter =true;? },? hide(state) {//同上state.showFooter =false;? },? newNum(state,sum){//同上,這里面的參數(shù)除了state之外還傳了需要增加的值sumstate.changableNum+=sum;? }};constactions = {? hideFooter(context) {//自定義觸發(fā)mutations里函數(shù)的方法撵儿,context與store 實例具有相同方法和屬性context.commit('hide');? },? showFooter(context) {//同上注釋context.commit('show');? },? getNewNum(context,num){//同上注釋校焦,num為要變化的形參context.commit('newNum',num)? }};conststore =newVuex.Store({? state,? getters,? mutations});exportdefaultstore;
vue提供了注入機制,就是把我們的store 對象注入到根實例中统倒。vue的根實例就是 new
Vue構(gòu)造函數(shù)寨典,然后在所有的子組件中this.$store 來指向store 對象。在index.js 中房匆,我們用export
store把store已經(jīng)暴露出去了耸成,然后直接在main.js中引入store并注入store即可。
// The Vue build version to load with the `import` command// (runtime-only or standalone) has been set in webpack.base.conf with an alias.importVuefrom'vue'importElementUIfrom'element-ui'import'element-ui/lib/theme-chalk/index.css'importAppfrom'./App'importrouterfrom'./router/router.js'importstorefrom'./store'importechartsfrom'echarts'Vue.config.productionTip =falseVue.use(ElementUI)Vue.use(echarts)Vue.prototype.$echarts = echarts/* eslint-disable no-new */newVue({el:'#app',? router,? store,components: { App },template:'<App/>'})
子組件中的computed屬性是根據(jù)它的依賴自動更新的浴鸿,所以只要store中的state發(fā)生變化井氢,它就會自動變化,在一般情況下子組件中獲取store中屬性的方式如下:
Count is {{某屬性}}
<script>
export default {
? computed: {
? count () {
? ? return this.$store.state.某屬性
? }
? }
}
</script>
通過computed屬性可以獲取到狀態(tài)值岳链,但是組件中每一個屬性(如:count)都是函數(shù)花竞,如果有10個,那么就要寫10個函數(shù)掸哑,且重復寫10遍return
this.$store.state不是很方便约急。vue 提供了mapState函數(shù),它把state直接映射到我們的組件中苗分。
當然使用mapState之前要先引入它厌蔽,它兩種用法,或接受一個對象摔癣,或接受一個數(shù)組奴饮,其中使用對象的方式又有三種方法。
對象用法如下:
import{mapState}from"vuex";// 引入mapState exportdefault{// 下面這三種寫法都可以computed: mapState({// 箭頭函數(shù)可使代碼更簡練count:state=>state.count,// 傳字符串參數(shù) 'count' 等同于 `state => state.count`countAlias:'count',// 為了能夠使用 `this` 獲取局部狀態(tài)择浊,必須使用常規(guī)函數(shù)countPlusLocalState (state) {returnstate.count +this.localCount? ? }? }) }
當映射的計算屬性的名稱與state的子節(jié)點名稱相同時戴卜,我們也可以給 mapState傳一個字符串數(shù)組。
import{mapState}from"vuex";exportdefault{computed: mapState([// 數(shù)組"count"]) }
如果我們組件內(nèi)部也有computed屬性怎么辦琢岩?它又不屬于mapState投剥,我們可以使用es6中的對象分割語法,把mapState函數(shù)生成的對象再分割成一個個的粘捎,就像最開始的時候我們一個一個羅列計算屬性薇缅,有10個屬性,我們就寫10個函數(shù)攒磨。
import{mapState}from"vuex";exportdefault{computed: {? ? ? ...mapState(["count"]),? ? ? getValue(){return1;? ? ? ? }? ? } }
二泳桦、Module中使用mapState、mapGetters娩缰、mapActions輔助函數(shù):
在src目錄下建立store文件夾:
?
其中:
collection.js
//collection.jsconststate={collects:['hi'],//初始化一個colects數(shù)組field:'空天作戰(zhàn)任務規(guī)劃'};constgetters={};constmutations={};constactions={};exportdefault{namespaced:true,//用于在全局引用此文件里的方法時標識這一個的文件名state,? getters,? mutations,? actions}
footerStatus.js:
//footerStatus.jsconststate={//要設置的全局訪問的state對象name:'beautiful',address:'Hunan Changsha',school:'國防科大',showFooter:true,changableNum:0//要設置的初始屬性值};constgetters = {//實時監(jiān)聽state值的變化(最新狀態(tài))};constmutations = {? changeSchool(state, value){? ? state.school = value;? }};constactions = {? _changeSchool(context, value){? ? context.commit('changeSchool', value)? }};exportdefault{namespaced:true,//用于在全局引用此文里的方法時標識這一個的文件名state,? getters,? mutations,? actions}
index.js:
importVuefrom'vue'importVuexfrom'vuex'importcollectionfrom'./modules/collection'importfooterStatusfrom'./modules/footerStatus'Vue.use(Vuex)exportdefaultnewVuex.Store({modules: {? ? collection,? ? footerStatus? }})
假如我們想在組件中使用module中的state灸撰、getters、mutations拼坎、actions浮毯,那該如何使用呢?
除了和普通store一樣需要在main.js中注入store外泰鸡,具體方法如下:
name: {{name}}
school: {{school}}
address: {{address}}
field: {{field}}
arrList: {{arrList}}
<script>
? import {mapState, mapGetters} from 'vuex'
? export default {
? ? data(){
? ? ? return {
? ? ? ? use: 'vuex高級使用方法'
? ? ? }
? ? },
? ? computed: {
? ? ? ...mapState({
? ? ? ? name: state => state.footerStatus.name,
? ? ? ? address(state){
? ? ? ? ? return state.footerStatus.address;
? ? ? ? }
? ? ? }),
? ? ? ...mapState('footerStatus', {
? ? ? ? school: 'school'
? ? ? }),
? ? ? ...mapState('collection', ['field']),
? ? ? _use(){
? ? ? ? this.use;
? ? ? },
? ? ? ...mapGetters('collection', {
? ? ? ? arrList: 'renderCollects'
? ? ? })
? ? },
? ? methods: {
? ? ? changeSchool(){
? ? ? ? this.$store.dispatch("footerStatus/_changeSchool", '北大');
? ? ? }
? ? }
? }
</script>
<style scoped>
</style>