vuex临时代码

main
roger_home_pc 1 year ago
parent f029dffb44
commit 2f937141f8
  1. 6
      src/store/index.js
  2. 21
      src/views/CompositionAPIView.vue
  3. 23
      src/views/OptionsAPIView.vue

@ -12,5 +12,11 @@ export default createStore({
moreCount: (state) => (n) => {
return state.count * n
}
},
mutations: {
// payload用于携带参数
increment(state, payload) {
state.count += payload.num
}
}
})

@ -1,17 +1,36 @@
<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>
<!-- 对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>
</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})
}
// mutations typemutations
const inc2 = () => {
store.commit({type: "increment", num: 5})
}
</script>

@ -1,17 +1,40 @@
<template>
<!-- 对state的操作 -->
<p><span><b>this.$store.state.count: </b></span>{{ this.$store.state.count }}</p>
<p><span><b>调用computed(halfCount): </b></span>{{ halfCount }}</p>
<!-- 对getters的操作分为无参和传参 -->
<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>
<!-- 对mutations的操作 -->
<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>
</template>
<script>
export default {
name: "OptionsAPIView",
// computedstate
computed: {
halfCount: function () {
return this.$store.state.count * 0.5
}
},
methods: {
// mutations
inc: function () {
this.$store.commit('increment', {num: 7})
},
// mutations typemutations
inc2: function () {
this.$store.commit({
type: "increment",
num: 5
})
}
}
}
</script>

Loading…
Cancel
Save