Vuex从仓库的创建到组件内的使用

Vuex基本使用

vuex中,有默认的五种基本的对象:

  • state:存储状态(变量)
  • getters:对数据获取之前的再次编译,可以理解为state的计算属性。我们在组件中使用 $sotre.getters.fun()
  • mutations:修改状态,并且是同步的。在组件中使用$store.commit(’’,params)。这个和我们组件中的自定义事件类似。
  • actions:异步操作。在组件中使用是$store.dispath(’’)
  • modules:store的子模块,为了开发大型项目,方便状态管理而使用的。这里我们就不解释了,用起来和上面的一样。

仓库建立方式:

store文件夹布局如下:

modules
--- app.js
--- index.js
getters.js
index.js

main.js

import Vue from "vue";
import Vuex from "vuex";
import modules from "./modules";
import getters from "./getters";

Vue.use(Vuex);
const debug = process.env.NODE_ENV !== "production";

export default new Vuex.Store({
  modules,
  getters,
  strict: debug
});

modules下的index.js

import app from "./app";

export default {
  app
};

moules下的app.js

import Cache from '@/utils/cache' //引入文件
// 存储状态变量
const state = {
	config: {},
	userInfo: {
		user_money: 0,
		user_integral: 0,
		coupon: 0
	},
	token: Cache.get(TOKEN) || null,
	...
};
// 修改状态
const mutations = {
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(TOKEN, opt.token, 60 * 24 * 60 * 60);//执行的其他操作
	},
	...
};
// 异步操作
const actions = {
	getCartNum({
		state,
		commit
	}) {
		return new Promise(resolve => {
			if (!state.token) return
			getCartNum().then(res => {
				if (res.code == 1) {
					commit('SETCARTNUM', res.data.num);//通过commit调取mutations的方法进而修改状态
					if (!res.data.num) return uni.removeTabBarBadge({
						index: 2
					})
					uni.setTabBarBadge({
						index: 2,
						text: String(res.data.num)
					})
					resolve()
				}
			})
		})
	},
	getUser({
		state,
		commit
	}) {
		return new Promise(resolve => {
			getUser().then(res => {
				if (res.code == 1) {
					commit('SETUSERINFO', res.data)
				}
				resolve()
			})
		})
	}
};
export default {
	state,
	mutations,
	actions
};

getter.js

export default {
  userInfo: state => state.app.userInfo || {},
  token: state => state.app.token,
  isLogin: state => !!state.app.token,
  cartNum: state => state.app.cartNum,
  loginNum: state => state.app.loginNum,
  inviteCode: state => state.app.userInfo.distribution_code || "",
  appConfig: state => state.app.config
};

组件中使用

页面中使用:

{{ $store.state.name }}

方法及值引入:

import {
	mapState,	
	mapGetters,
	mapActions,
	mapMutations
} from 'vuex'

方法使用:

	//下述中的 ... 是拓展运算符
    // 使用 [] 是解构赋值
    methods:{
		...mapMutations(['LOGIN']),
		...mapActions(['getUser']),
		logout() {
			this.$store.state.name;//仓库值的获取
			this.$store.commit("LOGOUT"); //同步操作-仓库值修改--基于mutations
			this.$store.dispatch("LOGOUT"); //异步方法调取--基于actions 
			this.$store.getters.fullInfo;//从获取到经过getter处理后的值
			
		}
    },
	computed: {
		...mapGetters(["cartNum", "inviteCode"])
		...mapState(['token'])
	}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue.js 2使用Vuex划分仓库后,你可以按照模块的方式组织你的数据和相关逻辑。这样可以使你的代码更加结构化和模块化。 首先,你需要在创建store实例时使用`modules`选项来划分仓库。每个模块都有自己的state、mutations、actions和getters。 以下是划分仓库的示例代码: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const moduleA = { state: { // 模块A的state countA: 0 }, mutations: { // 模块A的mutations incrementA(state) { state.countA++ } }, actions: { // 模块A的actions incrementA(context) { context.commit('incrementA') } }, getters: { // 模块A的getters getCountA: state => state.countA } } const moduleB = { state: { // 模块B的state countB: 0 }, mutations: { // 模块B的mutations incrementB(state) { state.countB++ } }, actions: { // 模块B的actions incrementB(context) { context.commit('incrementB') } }, getters: { // 模块B的getters getCountB: state => state.countB } } const store = new Vuex.Store({ modules: { moduleA, moduleB } }) export default store ``` 现在,你可以在你的Vue组件按模块的方式来存取数据: 1. 使用`this.$store.state.moduleA`来获取模块A的state数据: ```javascript this.$store.state.moduleA.countA ``` 2. 使用`this.$store.commit`来触发模块A的mutations方法,修改state数据: ```javascript this.$store.commit('moduleA/incrementA') ``` 3. 使用`this.$store.dispatch`来触发模块A的actions方法,间接地修改state数据: ```javascript this.$store.dispatch('moduleA/incrementA') ``` 4. 使用`this.$store.getters`来获取模块A的getters数据: ```javascript this.$store.getters['moduleA/getCountA'] ``` 同样地,你也可以使用类似的方式来存取模块B的数据。 通过这种方式,你可以更好地组织和管理你的Vuex仓库,使代码更加清晰和可维护。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeviesBob

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值