完成 mapState mapGetters mapMutations mapActions

main
roger_home_pc 1 year ago
parent 8eb4930f6c
commit 7cbeff5925
  1. 2
      src/store/modules/ModuleB.js
  2. 157
      src/views/OptionsAPIView.vue

@ -18,6 +18,6 @@ export const moduleB = {
if (content.state.state_b > 50) {
content.commit('mutation_b', {num: payload.num})
}
}
},
}
}

@ -13,27 +13,44 @@
<!-- 对mutations的操作 -->
<h3>对mutations的操作</h3>
<p><span><b>-this.$store.commit('mutation', {num: 7}): </b></span>
<button @click="root_mutation">点一下根store+7</button>
<p><span><b>-@click="mutation({num: 7})": </b></span>
<button @click="mutation({num: 7})">点一下根store+7</button>
</p>
<p><span><b>A-this.$store.commit('mutation_a', {num: 8}): </b></span>
<button @click="moduleA_mutation">点一下state_a+8</button>
<p><span><b>A-@click="mutation_a({num: 8})": </b></span>
<button @click="mutation_a({num: 8})">点一下state_a+8</button>
</p>
<p><span><b>B-this.$store.commit('module_b/mutation_b', {num: 9}): </b></span>
<button @click="moduleB_mutation">点一下moduleB+9</button>
<p><span><b>B-@click="mutation_b({num: 9})": </b></span>
<button @click="mutation_b({num: 9})">点一下moduleB+9</button>
</p>
<!-- 对actions的操作 -->
<h3>对actions的操作</h3>
<p><span><b>-mutation: </b></span>
<button @click="root_action">如果store>50 点一下store+10</button>
<p><span><b>-@click="action({num: 7})": </b></span>
<button @click="action({num: 7})">如果store>50 点一下store+10</button>
</p>
<p><span><b>A-this.$store.dispatch('action_a', {num: 8}): </b></span>
<button @click="action_a">如果state_a>50 点一下state_a+8</button>
<p><span><b>A-@click="action_a({num: 8})": </b></span>
<button @click="action_a({num: 8})">如果state_a>50 点一下state_a+8</button>
</p>
<p><span><b>B-this.$store.dispatch('module_b/action_b', {num: 9}): </b></span>
<button @click="moduleB_action">如果state_b>50 点一下state_b+9</button>
<p><span><b>B-@click="action_b({num: 9})": </b></span>
<button @click="action_b({num: 9})">如果state_b>50 点一下state_b+9</button>
</p>
<h3>混合用法</h3>
<p><span><b>state_b_getter_a: (state, getters) => state.module_b.state_b + getters.getter_a: </b></span>{{
state_b_getter_a
}}</p>
<p><span><b>state_a_getter_b: (state, getters) => state.module_a.state_a + getters["module_b/getter_b"]: </b></span>{{
state_a_getter_b
}}</p>
<p><span><b>state_b_getter_b: (state, getters) => state.state_b + getters.getter_b: </b></span>{{ state_b_getter_b }}
</p>
<p><span><b>@click="mutation_a_count(100): </b></span>
<button @click="mutation_a_count(100)">如果state_a>50 点一下state_a+100</button>
</p>
<p><span><b>@click="action_b_count(200): </b></span>
<button @click="action_b_count(200)">如果state_b>50 点一下state_b+200</button>
</p>
</template>
<script>
@ -43,49 +60,87 @@ export default {
name: "OptionsAPIView",
// computed mapStatemapGetters
computed: {
// state, statemap
...mapState(["count", "state_a"]),
// state
...mapState("module_b", ["state_b"]),
// Getters
...mapGetters(["getter", "getter_a"]),
// Getters
...mapGetters("module_b", ["getter_b"])
// //
// // state
// ...mapState(["count", "state_a"]), // statemapstate
// // state
// ...mapState("module_b", ["state_b"]), //
//
// // Getters
// ...mapGetters(["getter", "getter_a"]),
// // Getters
// ...mapGetters("module_b", ["getter_b"]),
//
// statenamespacedstate
...mapState({
count: "count",
state_a: state => state.module_a.state_a,
// state_b: state => state.module_b.state_b
}),
// state
...mapState("module_b", {
state_b: state => state.state_b
}),
// Getters
...mapGetters({
getter: "getter",
getter_a: "getter_a"
}),
// Getters
...mapGetters("module_b", {
getter_b: "getter_b",
}),
//
...mapState({
state_b_getter_a: (state, getters) => state.module_b.state_b + getters.getter_a,
state_a_getter_b: (state, getters) => state.module_a.state_a + getters["module_b/getter_b"],
}),
...mapState("module_b", {
state_b_getter_b: (state, getters) => state.state_b + getters.getter_b,
})
},
methods: {
// mutations
...mapMutations(["mutation", "mutation_a"]),
// //
// // mutations
// ...mapMutations(["mutation", "mutation_a"]),
// // mutations
// ...mapMutations("module_b", ["mutation_b"]),
// // actions
// ...mapActions(["action", "action_a"]),
// // actions
// ...mapActions("module_b", ["action_b"]),
//
// mutations
...mapMutations({
mutation: "mutation",
mutation_a: "mutation_a",
}),
// mutations
...mapMutations("module_b", ["mutation_b"]),
// actions
...mapActions(["action", "action_a"]),
// actions
...mapActions("module_b", ["action_b"]),
...mapMutations("module_b", {
mutation_b: "mutation_b",
}),
// actions
...mapActions({
action: "action",
action_a: "action_a"
}),
// namespacedactions
...mapActions("module_b", {
action_b: "action_b"
}),
//
...mapMutations({
mutation_a_count: (commit, count) => commit("mutation_a", {num: count})
}),
...mapActions("module_b", {
action_b_count: (dispatch, count) => dispatch("action_b", {num: count})
})
// storemutation
root_mutation: function () {
this.$store.commit('mutation', {num: 7})
},
// moduleAmutation
moduleA_mutation: function () {
this.$store.commit('mutation_a', {num: 8})
},
// moduleBmutation
moduleB_mutation: function () {
this.$store.commit('mutation_b', {num: 9})
},
// storeaction
root_action: function () {
this.$store.dispatch('action', {num: 7})
},
// moduleAaction
moduleA_action: function () {
this.$store.dispatch('action_a', {num: 8})
},
// moduleBaction
moduleB_action: function () {
this.$store.dispatch('action_b', {num: 9})
}
}
}
</script>

Loading…
Cancel
Save