|
|
|
@ -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); |
|
|
|
|
// 属性方式映射vuex中的mutations,必须使用commit方法订阅mutations |
|
|
|
|
// 属性形式订阅mutations |
|
|
|
|
const inc = () => { |
|
|
|
|
store.commit('increment', {num: 7}) |
|
|
|
|
} |
|
|
|
|
// 对象形式订阅mutations, type是mutations的名称 |
|
|
|
|
const inc2 = () => { |
|
|
|
|
store.commit({type: "increment", num: 5}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|