- 首先安装Vuex
yarn add vuex
- 在main中引入并且在新建的store文件夹下的index.js文件中使用use注册,至此所有VueComponent、Vue实例中都可以看见$store
main.js
import Vue from "vue";
import App from "./App.vue";
import store from "@/store";
Vue.config.productionTip = false;
new Vue({
store,
render: (h) => h(App),
}).$mount("#app");
index.js
import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);
const state = {};
const actions = {};
const getters = {};
const mutations = {};
export default new Vuex.Store({
state,
actions,
getters,
mutations,
});
- 此处我们采用最简单的加减来演示不同的写法,不记录$sotre.的写法
store-->index.js
import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);
const state = {
count: 0,
};
const actions = {
addCount(context, value) {
context.commit("ADD_COUNT", value);
},
};
const getters = {
expandCount(state) {
return state.count * 10;
},
};
const mutations = {
["ADD_COUNT"](state, value) {
state.count += value;
},
};
export default new Vuex.Store({
state,
actions,
getters,
mutations,
});
mapState相当于
storeCount(){
return this.$store.state.count
}
- 第一种对象写法 key value
<template>
<div>
<div>
<span>{{ storeCount }}</span>
</div>
<button @click="onAddCount">点我加1</button>
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
//第一种对象写法 key value
...mapState({ storeCount: "count" }),
},
methods: {
onAddCount() {
this.$store.dispatch("addCount", 1);
},
},
};
</script>
- 第二种数组写法(用于同名)
<template>
<div>
<div>
<span>{{ count }}</span>
</div>
<button @click="onAddCount">点我加1</button>
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
//第二种数组写法
...mapState(["count"]),
},
methods: {
onAddCount() {
this.$store.dispatch("addCount", 1);
},
},
};
</script>
mapGetters使用方法等同mapState
mapActions使用方法同mapMutations,此处以mapMutations为例
- 第一种对象写法
<template>
<div>
<div>
<span>{{ count }}</span>
<span>{{ expandCount }}</span>
</div>
<button @click="onAddCount(1)">点我加1</button>
</div>
</template>
<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
...mapState(["count"]),
...mapGetters(["expandCount"]),
},
methods: {
//基础写法 onAddCount不必在template中传参,
// onAddCount() {
// this.$store.commit("ADD_COUNT", 1);
// },
// mapMutations相当于 这就导致要在template中点击按钮的时候传入参数
// onAddCount(value) {
// this.$store.commit("ADD_COUNT", value);
// },
// 对象写法
...mapMutations({ onAddCount: "ADD_COUNT" }),
},
};
</script>
- 第二种数组写法
<template>
<div>
<div>
<span>{{ count }}</span>
<span>{{ expandCount }}</span>
</div>
<button @click="ADD_COUNT(1)">点我加1</button>
</div>
</template>
<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
...mapState(["count"]),
...mapGetters(["expandCount"]),
},
methods: {
//基础写法 onAddCount不必在template中传参,
// onAddCount() {
// this.$store.commit("ADD_COUNT", 1);
// },
// mapMutations相当于 这就导致要在template中点击按钮的时候传入参数
// onAddCount(value) {
// this.$store.commit("ADD_COUNT", value);
// },
// 对象写法
// ...mapMutations({ onAddCount: "ADD_COUNT" }),
//数组写法
...mapMutations(["ADD_COUNT"]),
},
};
</script>
Vuex模块化
重构store中index.js代码,此处为了简便,不单独拆分文件
import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);
const addOptions = {
//开启命名空间,因为getters actions不区分模块
namespaced: true,
state: {
count: 0,
},
actions: {
addCount(context, value) {
context.commit("ADD_COUNT", value);
},
},
getters: {
expandCount(state) {
return state.count * 10;
},
},
mutations: {
["ADD_COUNT"](state, value) {
state.count += value;
},
},
};
const subOptions = {
namespaced: true,
state: {
count: 0,
},
actions: {
subCount(context, value) {
context.commit("SUB_COUNT", value);
},
},
getters: {
expandCount(state) {
return state.count * 10;
},
},
mutations: {
["SUB_COUNT"](state, value) {
state.count -= value;
},
},
};
export default new Vuex.Store({
modules: {
customAdd: addOptions,
customSub: subOptions,
},
});
- mapState基础实现
<template>
<div>
<div>
<span>{{ customAdd.count }}</span>
</div>
<!-- <button @click="onAddCount">点我加1</button> -->
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
//取自store下index.js中modules的key
...mapState(["customAdd"]),
},
methods: {
// onAddCount() {
// this.$store.dispatch("addCount", 1);
// },
},
};
</script>
展开$store看下现在的格式,所以要在template中采用customAdd.count去取值
- 进阶写法
这种写法必须在store下index.js开启namespaced:true才可以生效
<template>
<div>
<div>
<span>{{ count }}</span>
</div>
<!-- <button @click="onAddCount">点我加1</button> -->
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
...mapState("customAdd", ["count"]),
},
methods: {
// onAddCount() {
// this.$store.dispatch("addCount", 1);
// },
},
};
</script>
- mapMutations和mapState相似
<template>
<div>
<div>
<span>{{ count }}</span>
</div>
<button @click="onAddCount(1)">点我加1</button>
</div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
...mapState("customAdd", ["count"]),
},
methods: {
...mapMutations("customAdd", { onAddCount: "ADD_COUNT" }),
},
};
</script>
- mapGetters、mapActions、mapMutations、mapState都相似写法
<template>
<div>
<div>
<span>{{ count }}</span>
<span>{{ expandCount }}</span>
</div>
<button @click="onAddCount(1)">点我加1</button>
</div>
</template>
<script>
import { mapState, mapActions, mapGetters } from "vuex";
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
...mapState("customAdd", ["count"]),
...mapGetters("customAdd", ["expandCount"]),
},
methods: {
...mapActions("customAdd", { onAddCount: "addCount" }),
},
};
</script>
- 不借助map,初始化写法如何写?整合展示,只有getters有些特别,展开getters,与state结构不同
<template>
<div>
<div>
<span>{{ count }}</span>
<span>{{ expandCount }}</span>
</div>
<button @click="onAddCount">点我加1</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {};
},
computed: {
count() {
return this.$store.state.customAdd.count;
},
expandCount() {
return this.$store.getters["customAdd/expandCount"];
},
},
methods: {
onAddCount() {
// this.$store.commit("customAdd/ADD_COUNT", 1);
this.$store.dispatch("customAdd/addCount", 1);
},
},
};
</script>