vue中vuex的使用方法(详解)

文件概要

项目地址
在这里插入图片描述
index.js文件

import Vue from 'vue'
import Vuex from 'vuex'

import moduleA from './modules/moduleA'
import moduleB from './modules/moduleB'

Vue.use(Vuex)

export default new Vuex.Store({
	modules: {
		moduleA,
		moduleB
	},
	state: {
		"indexName": "名字",
		"indexFirstName": "主要第一名字",
		"indexLastName": "主要最后名字",
		"indexUser": {
			"userName": "主要人员名称",
			"age": 18
		}
	},
	getters: {
		allName: state => {
			return state.indexFirstName + state.indexLastName
		}
	},
	mutations: {
		SET_INDEX_NAME: (state, name) => {
			state.indexName = name
		},
	},
	actions: {
		changeName(context, newName) {
			context.commit("SET_INDEX_NAME", newName)
		}
	}
})

moduleA.js文件

export default {
	namespaced: true,
	state: {
		aName: "moduleA的名字",
		aNum: 1
	},
	getters: {
		aGetterName: state => {
			return state.aName + "aGetterName"
		}
	},
	mutations: {
		ADD_ANUM(state, num) {
			state.aNum = state.aNum + num
		}
	},
	actions: {
		addCountAction(context, num) {
			context.commit('ADD_ANUM', num)
		}
	}
}

一、基本用法

state属性:基本数据;
getters属性:从 state 中派生出的数据;
mutation属性:更新 store 中数据的唯一途径,其接收一个以 state 为第一参数的回调函数;
action 属性:提交 mutation 以更改 state,其中可以包含异步操作;
module 属性:用于将 store分割成不同的模块。

// 获取index的数据   state      getters
console.log("indexName", this.$store.state.indexName)
console.log("indexUser", this.$store.state.indexUser)

console.log("allName", this.$store.getters.allName)

// 修改index的数据   mutations  actions
this.$store.commit('SET_INDEX_NAME', "修改主要名字")
console.log("indexName", this.$store.state.indexName)

this.$store.dispatch("changeName", "再次修改主要名字")
console.log("indexName", this.$store.state.indexName)

打印结果
在这里插入图片描述

二、模块化

我的文章

// 获取moduleA的数据   state      getters
console.log("moduleA  aName", this.$store.state.moduleA.aName)
console.log("moduleA  aNum", this.$store.state.moduleA.aNum)

console.log("moduleA  aGetterName", this.$store.getters['moduleA/aGetterName'])

// 修改moduleA的数据   mutations  actions
this.$store.commit('moduleA/ADD_ANUM', 10);
console.log("moduleA  aNum", this.$store.state.moduleA.aNum)

this.$store.dispatch('moduleA/addCountAction', 10)
console.log("moduleA  aNum", this.$store.state.moduleA.aNum)

打印结果
在这里插入图片描述

三、mapState、mapMutations、mapActions、mapGetters具体用法

参考文档01
参考文档02

mapState

import { mapState } from 'vuex'
export default {
  computed: {
    // ...mapState(["indexName", "indexUser"])
    ...mapState({
      newIndexName: "indexName",
      newIndexUser: "indexUser",
      // 模块化使用方式
      newAName: state => state.moduleA.aName,
    })
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      // console.log('indexName', this.indexName)
      // console.log('indexUser', this.indexUser)
      console.log('newIndexName', this.newIndexName)
      console.log('newIndexUser', this.newIndexUser)
      console.log('newAName', this.newAName)
    }
  },
}

mapGetters

import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters({
      newAllName: "allName",
    }),
    ...mapGetters('moduleA', {
      newAGetterName: "aGetterName",
    }),
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      console.log('newAllName', this.newAllName)
      console.log('newAGetterName', this.newAGetterName)
    }
  },
}

mapMutations

<button @click="changeIndexName('变更主要姓名')">changeIndexName</button>
<button @click="changeAnum">changeAnum</button>
import { mapMutations } from 'vuex'
export default {
  methods: {
    ...mapMutations({
      setIndexName: "SET_INDEX_NAME"
    }),
    ...mapMutations('moduleA', {
      addAnum: "ADD_ANUM"
    }),
    changeIndexName (val) {
      this.setIndexName(val)
      console.log('indexName', this.$store.state.indexName)
    },
    changeAnum () {
      this.addAnum(100)
      console.log('indexName', this.$store.state.moduleA.aNum)
    },
  },
}

mapActions

<button @click="changeNameBtn">changeNameBtn</button>
<button @click="addCountActionBtn">addCountActionBtn</button>
import { mapActions } from 'vuex'
export default {
  methods: {
    ...mapActions({
      changeName: "changeName"
    }),
    ...mapActions('moduleA', {
      addCountAction: "addCountAction"
    }),
    changeNameBtn () {
      this.changeName("再次变更主要姓名")
      console.log('indexName', this.$store.state.indexName)
    },
    addCountActionBtn () {
      this.addCountAction(210)
      console.log('indexName', this.$store.state.moduleA.aNum)
    },
  },
}

四、表单处理(v-model)

<input v-model="message">
computed: {
  message: {
    get () {
      return this.$store.state.obj.message
    },
    set (value) {
      this.$store.commit('updateMessage', value)
    }
  }
}
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值