完成modules的学习

main
RogerWork 1 year ago
parent c95e81ef13
commit e465b660f2
  1. 8
      src/store/modules/ModuleA.js
  2. 6
      src/store/modules/ModuleB.js
  3. 84
      src/views/CompositionAPIView.vue
  4. 108
      src/views/OptionsAPIView.vue

@ -9,12 +9,14 @@ export const moduleA = {
},
mutations: {
mutation_a(state, payload) {
state.state_a + payload.num;
state.state_a += payload.num;
}
},
actions: {
action_a(content, payload){
content.commit('mutation_a', {num: payload.num})
action_a(content, payload) {
if (content.state.state_a > 50) {
content.commit('mutation_a', {num: payload.num})
}
}
}
}

@ -10,12 +10,14 @@ export const moduleB = {
},
mutations: {
mutation_b: (state, payload) => {
state.state_b + payload.num;
state.state_b += payload.num;
}
},
actions: {
action_b: (content, payload) => {
content.commit('mutation_b', {num: payload.num})
if (content.state.state_b > 50) {
content.commit('mutation_b', {num: payload.num})
}
}
}
}

@ -1,49 +1,75 @@
<template>
<!-- 对state的操作 -->
<p><span><b>store.state.count: </b></span>{{ store.state.count }}</p>
<p><span><b>调用computed(halfCount): </b></span>{{ halfCount }}</p>
<!-- 对getters的操作分为无参和传参 -->
<p><span><b>store.getters.doubleCount: </b></span>{{ store.getters.doubleCount }}</p>
<p><span><b>store.getters.moreCount(10): </b></span>{{ store.getters.moreCount(10) }}</p>
<h3>对state的操作</h3>
<p><span><b>-store.state.count: </b></span>{{ store.state.count }}</p>
<p><span><b>A-store.state.state_a: </b></span>{{ store.state.module_a.state_a }}</p>
<p><span><b>B-store.state.module_b.state_b: </b></span>{{ store.state.module_b.state_b }}</p>
<!-- 对getters的操作 -->
<h3>对getters的操作</h3>
<p><span><b>-store.getters.getter(3): </b></span>{{ store.getters.getter(3) }}</p>
<p><span><b>A-store.getters.getter_a: </b></span>{{ store.getters.getter_a }}</p>
<p><span><b>B-store.getters['module_b/getter_b']: </b></span>{{ store.getters['module_b/getter_b'] }}</p>
<!-- 对mutations的操作 -->
<p><span><b>store.commit('increment', {num: 7}): </b></span>
<button @click="inc">点一下count+7</button>
</p>
<p><span><b>store.commit({type: "increment", num: 5}): </b></span>
<button @click="inc2">点一下count+5</button>
<h3>对mutations的操作</h3>
<p><span><b>-store.commit('mutation', {num: 7}): </b></span>
<button @click="root_mutation">点一下根store+7</button>
</p>
<!-- 对actions的操作 -->
<p><span><b>store.commit({type: "increment", num: 5}): </b></span>
<button @click="inc_action1">如果count>50 点一下count+10</button>
<p><span><b>A-store.commit('mutation_a', {num: 8}): </b></span>
<button @click="moduleA_mutation">点一下state_a+8</button>
</p>
<p><span><b>store.dispatch({type: 'inc_actions', num: 5}): </b></span>
<button @click="inc_action2">如果count>50 点一下count+8</button>
<p><span><b>B-store.commit('module_b/mutation_b', {num: 9}): </b></span>
<button @click="moduleB_mutation">点一下state_b+9</button>
</p>
<!-- 对actions的操作 -->
<h3>对actions的操作</h3>
<p><span><b>-store.dispatch('action', {num: 7}): </b></span>
<button @click="root_action">如果store>50 点一下store+10</button>
</p>
<p><span><b>A-store.dispatch('action_a', {num: 8}): </b></span>
<button @click="moduleA_action">如果state_a>50 点一下state_a+8</button>
</p>
<p><span><b>B-store.dispatch('module_b/action_b', {num: 9}): </b></span>
<button @click="moduleB_action">如果state_b>50 点一下state_b+9</button>
</p>
</template>
<script setup>
import {computed} from "vue";
import {useStore} from "vuex";
// useStore
const store = useStore()
// vuex
const halfCount = computed(() => store.state.count * 0.5);
// vuexmutations使commitmutations
// mutations
const inc = () => {
store.commit('increment', {num: 7})
// storemutation
const root_mutation = () => {
store.commit('mutation', {num: 7})
}
// mutations typemutations
const inc2 = () => {
store.commit({type: "increment", num: 5})
// moduleAmutation
const moduleA_mutation = () => {
store.commit('mutation_a', {num: 8})
}
const inc_action1 = () => {
store.dispatch('inc_actions', {num: 7})
// moduleBmutation
const moduleB_mutation = () => {
store.commit('module_b/mutation_b', {num: 9})
}
// storeaction
const root_action = () => {
store.dispatch('action', {num: 7})
}
// moduleAaction
const moduleA_action = () => {
store.dispatch('action_a', {num: 8})
}
const inc_action2 = () => {
store.dispatch({type: 'inc_actions', num: 5})
// moduleBaction
const moduleB_action = () => {
store.dispatch('module_b/action_b', {num: 9})
}
</script>

@ -1,59 +1,73 @@
<template>
<!-- 对state的操作 -->
<p><span><b>根store-this.$store.state.count: </b></span>{{ this.$store.state.count }}</p>
<p><span><b>moduleA-this.$store.state.module_a.state_a: </b></span>{{ this.$store.state.module_a.state_a }}</p>
<p><span><b>moduleA-this.$store.state.module_b.state_b: </b></span>{{ this.$store.state.module_b.state_b }}</p>
<!-- <p><span><b>moduleB-this.$store.state.count: </b></span>{{ this.$store.module_b.state.state_b }}</p>-->
<h3>对state的操作</h3>
<p><span><b>-this.$store.state.count: </b></span>{{ this.$store.state.count }}</p>
<p><span><b>A-this.$store.state.module_a.state_a: </b></span>{{ this.$store.state.module_a.state_a }}</p>
<p><span><b>B-this.$store.state.module_b.state_b: </b></span>{{ this.$store.state.module_b.state_b }}</p>
<!-- 对getters的操作 -->
<h3>对getters的操作</h3>
<p><span><b>根this.$store.getters.getter(3): </b></span>{{ this.$store.getters.getter(3) }}</p>
<p><span><b>A-this.$store.getters.getter_a: </b></span>{{ this.$store.getters.getter_a }}</p>
<p><span><b>B-this.$store.getters['module_b/getter_b']: </b></span>{{
this.$store.getters['module_b/getter_b']
}}</p>
<!-- &lt;!&ndash; 对getters的操作分为无参和传参 &ndash;&gt;-->
<!-- <p><span><b>this.$store.getters.doubleCount: </b></span>{{ this.$store.getters.doubleCount }}</p>-->
<!-- <p><span><b>this.$store.getters.moreCount(10): </b></span>{{ this.$store.getters.moreCount(10) }}</p>-->
<!-- &lt;!&ndash; 对mutations的操作 &ndash;&gt;-->
<!-- <p><span><b>this.$store.commit('increment', {num: 7})}: </b></span>-->
<!-- <button @click="inc">点一下count+7</button>-->
<!-- </p>-->
<!-- <p><span><b>this.$store.commit({type: "increment", num: 5})}: </b></span>-->
<!-- <button @click="inc2">点一下count+5</button>-->
<!-- </p>-->
<!-- &lt;!&ndash; 对actions的操作 &ndash;&gt;-->
<!-- <p><span><b>this.$store.dispatch('inc_actions', {num: 7})}: </b></span>-->
<!-- <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>-->
<!-- 对mutations的操作 -->
<h3>对mutations的操作</h3>
<p><span><b>-this.$store.commit('mutation', {num: 7}): </b></span>
<button @click="root_mutation">点一下根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>
<p><span><b>B-this.$store.commit('module_b/mutation_b', {num: 9}): </b></span>
<button @click="moduleB_mutation">点一下moduleB+9</button>
</p>
<!-- 对actions的操作 -->
<h3>对actions的操作</h3>
<p><span><b>-this.$store.dispatch('action', {num: 7}): </b></span>
<button @click="root_action">如果store>50 点一下store+10</button>
</p>
<p><span><b>A-this.$store.dispatch('action_a', {num: 8}): </b></span>
<button @click="moduleA_action">如果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>
</template>
<script>
export default {
name: "OptionsAPIView",
// computedstate
// computed: {
// halfCount: function () {
// console.log(this.$store)
// return this.$store.state_a
// }
// },
// methods: {
// // mutations
// inc: function () {
// this.$store.commit('increment', {num: 7})
// },
// // mutations typemutations
// inc2: function () {
// this.$store.commit({
// type: "increment",
// num: 5
// })
// },
// inc_action1: function () {
// this.$store.dispatch('inc_actions', {num: 7})
// },
// inc_action2: function () {
// this.$store.dispatch({type: 'inc_actions', num: 5})
// }
// }
methods: {
// 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('module_b/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('module_b/action_b', {num: 9})
}
}
}
</script>

Loading…
Cancel
Save