Vue单页面使用Vuex汇总

单页面使用

单页面使用 state
//一、直接获取
this.$store.user.userName
//二、利用辅助函数
	//首先引入辅助函数
	import { mapState } from "vuex";
	//创建computed来获取
	export default {
		computed:{
			//mapState([模块名称],{
			//	监听者变量:所实时监听的状态数据
			//})
			// 官网说法:把 `this.usName ` 映射为 `this.$store.user.userName`
			...mapState('user/',{
				usName : s => s.userName
			}),
		}
	}
	//页面使用,如同在data内创建 usName 参数,使用如下:
	this.usName
单页面使用 getters
//首先引入辅助函数
import { mapGetters } from "vuex";
//创建computed来获取
export default {
	computed:{
		//mapGetters ([模块名称],{
		//	监听者变量:所实时监听的状态数据
		//})
		// 官网说法:把 `this.usName ` 映射为 `this.$store.user.userName`
		...mapGetters ('user/',{
			usName : 'userName'
		}),
	}
}
//页面使用,如同在data内创建 usName 参数,使用如下:
this.usName
单页面使用 mutations
import { mapMutations } from 'vuex'
export default {
  methods: {
    ...mapMutations([
      'increment', 
      // 将 `this.increment()` 映射为 `this.$store.commit('increment')`

      // `mapMutations` 也支持载荷:
      'incrementBy' 
      // 将 `this.incrementBy(amount)` 
      // 映射为 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' 
      // 将 `this.add()` 映射为 `this.$store.commit('increment')`
    })
  }
}
//使用:
this.increment()
this.incrementBy()
this.add()
单页面使用 actions
import { mapActions } from 'vuex'

export default {
  // ...
  methods: {
    ...mapActions([
      'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`

      // `mapActions` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
    })
  }
}
//使用:
this.increment()
this.incrementBy()
this.add()

单模块使用

在vuex单模块内综合使用
//vuex模板代码
state:{
	count:0
}
mutations: {
    someMutation(state) {
      // 变更状态
      state.count++
    }
}
actions: {
  actionA ({ commit }) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        commit('someMutation')
        resolve()
      }, 1000)
    })
  },
  actionB ({ dispatch, commit }) {
    return dispatch('actionA').then(() => {
      commit('someOtherMutation')
    })
  }
  // async + await 
		async actionA ({ commit }) {
    	commit('gotData', await getData())
	  },
	  async actionB ({ dispatch, commit }) {
	    await dispatch('actionA') // 等待 actionA 完成
	    commit('gotOtherData', await getOtherData())
	  }
}

跨模块使用

store 普及
其中有:
	commit, 		//执行:mutations 
	dispatch, 		//执行:actions 
	state, 			//访问当前模块state
	rootState, 		//访问Vuex的所有模块state
	rootGetters, 	//访问Vuex的所有模块getters
	getters 		//访问当前模块getters
state
const actions = {
    async login(store, config = {}) {
        const { rootState } = store
		rootState.user.userName
		//rootState.模块名.attrName
    }
}
getters
const actions = {
    async login(store, config = {}) {
        const { rootGetters } = store
		rootGetters['vip/get']
		//rootGetters[路径]
    }
}
mutations
const actions = {
    async login(store, config = {}) {
        const { commit } = store
		commit('vip/receive', data, {root: true}) // 调用其他模块的 mutations
		//commit(方法路径,数据(没数据预留),配置项(告诉vue这非本模块方法))
    }
}
actions
const actions = {
    async login(store, config = {}) {
        const { dispatch } = store
		dispatch('vip/get', {}, {root: true}) // 调用其他模块的 actions
		//dispatch(方法路径,数据(没数据预留),配置项(告诉vue这非本模块方法))
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue 3中使用Vuex,你需要按照以下步骤进行设置: 1. 安装Vuex:在终端中运行以下命令安装Vuex: ``` npm install vuex@next ``` 2. 创建一个Vuex store:在你的项目中创建一个新的文件(例如store.js),并导入`createStore`函数和你的模块文件(如果有的话): ```javascript import { createStore } from 'vuex'; import moduleA from './moduleA'; export default createStore({ modules: { moduleA } }); ``` 3. 创建模块(可选):如果你的应用程序需要使用模块来组织状态,你可以创建一个或多个模块文件。在上面的示例中,我们导入了一个名为moduleA的模块。 4. 在Vue应用程序中使用Vuex:在你的main.js文件中导入创建的store,并将其与Vue应用程序实例关联: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import store from './store'; const app = createApp(App); app.use(store); app.mount('#app'); ``` 现在,你可以在你的Vue组件中使用Vuex的功能,包括状态管理、mutations、actions和getters等。 例如,在组件中访问和修改状态: ```javascript import { useStore } from 'vuex'; export default { setup() { const store = useStore(); // 访问状态 const count = computed(() => store.state.moduleA.count); // 修改状态 const increment = () => { store.commit('moduleA/increment'); }; return { count, increment }; } }; ``` 这就是在Vue 3中使用Vuex的基本步骤。你可以在官方文档中了解更多关于Vuex的信息:https://next.vuex.vuejs.org/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值