前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

vuex

作者头像
阿超
发布2022-08-17 21:40:11
9250
发布2022-08-17 21:40:11
举报
文章被收录于专栏:快乐阿超快乐阿超

人之贤不肖譬如鼠矣,在所自处耳!――《李斯列传》

聊聊vuex,官方文档:https://vuex.vuejs.org/zh/

介绍就不赘述了,直接上使用

安装:

代码语言:javascript
复制
cnpm install vuex --save
image-20211122183023983
image-20211122183023983

我们新建一个store,再创建一个index.js

再新建一个modules目录,里面放上value.js

image-20211122183125626
image-20211122183125626

main.js中我们写入

image-20211122183205337
image-20211122183205337
代码语言:javascript
复制
import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
import store from '@/store';

Vue.config.productionTip = false

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')

挂载完毕后,我们编写main.jsvalue.js

main.js

代码语言:javascript
复制
// 页面路径:store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

// vue的插件机制
Vue.use(Vuex);

import ruben from '@/store/modules/value.js' 
//Vuex.Store 构造器选项
export default new Vuex.Store({
	modules: {
		// 模块
		ruben
	}
});

value.js

代码语言:javascript
复制
// vuex module
// 在外部使用`vuex`,可以如下引用
// import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
export default {
	// namespaced:true,
	// `state` 在外部访问可以使用 `mapState`
	// computed: {
	//	...mapState({
	//		value: state => state.ruben.value
	//	}),
	// }
	// 配置了`mapState`就可以使用 `this.value` 代替 `this.$store.state.ruben.value`
	state: {
		value: 'hello'
	},
	// `getters` 在外部访问可以使用 `MapGetters`
	// computed: {
	// 	...mapGetters(['getValue']) 
	// }
	// 配置了`MapGetters`就可以使用 `getValue` 代替 `this.$store.getters.getValue`
	// 取别名: 把 `this.getVuexValue` 映射为 `this.$store.getters.getValue`
	//	...mapGetters({getVuexValue:'getValue'})
	getters: {
		getValue: state => state.value
	},
	// `mutations` 在外部访问可以使用 `mapMutations`
	// methods:{
	// 	...mapMutations(['setValue'])
	// }
	// 配置了`mapMutations`就可以使用 `setValue(value)` 代替 `this.$store.commit('setValue', value)`
	// 取别名: 把 `this.setVuexValue(value)` 映射为 `this.$store.commit('setValue', value)`
	//	...mapMutations({setVuexValue:'setValue'})
	mutations: {
		setValue(state, value) {
			state.value = value
		}
	},
	// `actions` 在外部访问可以使用 `mapActions`
	// methods:{
	// 	...mapActions(['submitValue'])
	// }
	// 配置了`mapActions`就可以使用 `submitValue()` 代替 `this.$store.dispatch('submitValue', value)`
	// 取别名: 把 `this.submitVuexValue(value)` 映射为 `this.$store.dispatch('submitValue', value)`
	//	...mapMutations({submitVuexValue:'setValue'})
	actions: {
		submitValue(context, value) {
			console.log("context: ", context);
			return new Promise((resolve, reject) => setTimeout(() => {
				context.commit('setValue', value);
				resolve(value);
			}, 1000))
		}
	}
}

去掉注释长这样:

代码语言:javascript
复制
export default {
	state: {
		value: 'hello'
	},
	getters: {
		getValue: state => state.value
	},
	mutations: {
		setValue(state, value) {
			state.value = value
		}
	},
	actions: {
		submitValue(context, value) {
			console.log("context: ", context);
			return new Promise((resolve, reject) => setTimeout(() => {
				context.commit('setValue', value);
				resolve(value);
			}, 1000))
		}
	}
}

然后我们找个页面引用一下

代码语言:javascript
复制
<template></template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
export default {
	computed: {
		...mapState({
			value: state => state.ruben.value
		}),
		...mapGetters(['getValue'])
	},
	methods: {
		...mapMutations(['setValue']),
		...mapActions(['submitValue'])
	},
	created() {
		console.log('this: ', this);
		
		// state
		console.log('this.value: ', this.value);
		console.log('this.$store.state.value: ', this.$store.state.ruben.value);
		
		// mutations
		this.setValue('ruben');
		this.$store.commit('setValue', 'ruben');
		
		// getters
		console.log('this.getValue: ', this.getValue);
		console.log('this.$store.getters.getValue: ', this.$store.getters.getValue);
		
		// actions
		this.submitValue('achao').then(console.log);
		this.$store.dispatch('submitValue','achao').then(console.log);
	}
};
</script>

<style></style>

我们看输出结果

image-20211122194025725
image-20211122194025725
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档