vuex临时代码

main
roger_home_pc 1 year ago
parent 3ea54bdbae
commit c95e81ef13
  1. 21
      src/store/index.js
  2. 20
      src/store/modules/ModuleA.js
  3. 21
      src/store/modules/ModuleB.js
  4. 91
      src/views/OptionsAPIView.vue

@ -1,32 +1,35 @@
import {createStore} from "vuex"; import {createStore} from "vuex";
import {moduleA} from "@/store/modules/ModuleA";
import {moduleB} from "@/store/modules/ModuleB";
export default createStore({ export default createStore({
modules: {
module_a: moduleA,
module_b: moduleB,
},
state: { state: {
count: 2, count: 3,
}, },
getters: { getters: {
// getter使用箭头函数来定义,默认必传参数时state,如果需要其余参数,可以使用嵌套头函数 // getter使用箭头函数来定义,默认必传参数时state,如果需要其余参数,可以使用嵌套头函数
doubleCount: (state) => { getter: (state) => (n) => {
return state.count * 2 return state.count * n;
}, },
moreCount: (state) => (n) => {
return state.count * n
}
}, },
mutations: { mutations: {
// payload用于携带参数 // payload用于携带参数
increment(state, payload) { mutation(state, payload) {
state.count += payload.num state.count += payload.num
} }
}, },
actions: { actions: {
inc_actions(content, payload) { action(content, payload) {
// 模拟异步操作 // 模拟异步操作
setTimeout(() => { setTimeout(() => {
// rep 模拟axios请求返回的数据 // rep 模拟axios请求返回的数据
const rep = 3; const rep = 3;
if (content.state.count > 50) { if (content.state.count > 50) {
content.commit('increment', {num: rep+payload.num}); content.commit('mutation', {num: rep + payload.num});
} }
}, 1000) }, 1000)
} }

@ -0,0 +1,20 @@
export const moduleA = {
state: {
state_a: 1,
},
getters: {
getter_a: (state) => {
return state.state_a + 10;
}
},
mutations: {
mutation_a(state, payload) {
state.state_a + payload.num;
}
},
actions: {
action_a(content, payload){
content.commit('mutation_a', {num: payload.num})
}
}
}

@ -0,0 +1,21 @@
export const moduleB = {
namespaced: true,
state: {
state_b: 2,
},
getters: {
getter_b: (state) => {
return state.state_b + 10;
}
},
mutations: {
mutation_b: (state, payload) => {
state.state_b + payload.num;
}
},
actions: {
action_b: (content, payload) => {
content.commit('mutation_b', {num: payload.num})
}
}
}

@ -1,54 +1,59 @@
<template> <template>
<!-- 对state的操作 --> <!-- 对state的操作 -->
<p><span><b>this.$store.state.count: </b></span>{{ this.$store.state.count }}</p> <p><span><b>根store-this.$store.state.count: </b></span>{{ this.$store.state.count }}</p>
<p><span><b>调用computed(halfCount): </b></span>{{ halfCount }}</p> <p><span><b>moduleA-this.$store.state.module_a.state_a: </b></span>{{ this.$store.state.module_a.state_a }}</p>
<!-- 对getters的操作分为无参和传参 --> <p><span><b>moduleA-this.$store.state.module_b.state_b: </b></span>{{ this.$store.state.module_b.state_b }}</p>
<p><span><b>this.$store.getters.doubleCount: </b></span>{{ this.$store.getters.doubleCount }}</p> <!-- <p><span><b>moduleB-this.$store.state.count: </b></span>{{ this.$store.module_b.state.state_b }}</p>-->
<p><span><b>this.$store.getters.moreCount(10): </b></span>{{ this.$store.getters.moreCount(10) }}</p>
<!-- 对mutations的操作 -->
<p><span><b>this.$store.commit('increment', {num: 7})}: </b></span> <!-- &lt;!&ndash; 对getters的操作分为无参和传参 &ndash;&gt;-->
<button @click="inc">点一下count+7</button> <!-- <p><span><b>this.$store.getters.doubleCount: </b></span>{{ this.$store.getters.doubleCount }}</p>-->
</p> <!-- <p><span><b>this.$store.getters.moreCount(10): </b></span>{{ this.$store.getters.moreCount(10) }}</p>-->
<p><span><b>this.$store.commit({type: "increment", num: 5})}: </b></span> <!-- &lt;!&ndash; 对mutations的操作 &ndash;&gt;-->
<button @click="inc2">点一下count+5</button> <!-- <p><span><b>this.$store.commit('increment', {num: 7})}: </b></span>-->
</p> <!-- <button @click="inc">点一下count+7</button>-->
<!-- 对actions的操作 --> <!-- </p>-->
<p><span><b>this.$store.dispatch('inc_actions', {num: 7})}: </b></span> <!-- <p><span><b>this.$store.commit({type: "increment", num: 5})}: </b></span>-->
<button @click="inc_action1">如果count>50 点一下count+10</button> <!-- <button @click="inc2">点一下count+5</button>-->
</p> <!-- </p>-->
<p><span><b>this.$store.dispatch({type: 'inc_actions', num: 5})}: </b></span> <!-- &lt;!&ndash; 对actions的操作 &ndash;&gt;-->
<button @click="inc_action2">如果count>50 点一下count+8</button> <!-- <p><span><b>this.$store.dispatch('inc_actions', {num: 7})}: </b></span>-->
</p> <!-- <button @click="inc_action1">如果count>50 点一下count+10</button>-->
<!-- </p>-->
<!-- <p><span><b>this.$store.dispatch({type: 'inc_actions', num: 5})}: </b></span>-->
<!-- <button @click="inc_action2">如果count>50 点一下count+8</button>-->
<!-- </p>-->
</template> </template>
<script> <script>
export default { export default {
name: "OptionsAPIView", name: "OptionsAPIView",
// computedstate // computedstate
computed: { // computed: {
halfCount: function () { // halfCount: function () {
return this.$store.state.count * 0.5 // console.log(this.$store)
} // return this.$store.state_a
}, // }
methods: { // },
// mutations // methods: {
inc: function () { // // mutations
this.$store.commit('increment', {num: 7}) // inc: function () {
}, // this.$store.commit('increment', {num: 7})
// mutations typemutations // },
inc2: function () { // // mutations typemutations
this.$store.commit({ // inc2: function () {
type: "increment", // this.$store.commit({
num: 5 // type: "increment",
}) // num: 5
}, // })
inc_action1: function () { // },
this.$store.dispatch('inc_actions', {num: 7}) // inc_action1: function () {
}, // this.$store.dispatch('inc_actions', {num: 7})
inc_action2: function () { // },
this.$store.dispatch({type: 'inc_actions', num: 5}) // inc_action2: function () {
} // this.$store.dispatch({type: 'inc_actions', num: 5})
} // }
// }
} }
</script> </script>

Loading…
Cancel
Save