提交 2f52f23e 编写于 作者: S sw_pc

Count组件测试

上级 89e94858
<template> <template>
<div id="app"> <div id="app">
<img alt="Vue logo" src="./assets/logo.png"> <img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/> <Count msg="Welcome to Your Vue.js App"/>
<HelloWorld id="hello" msg="Welcome to Your Vue.js App"/>
</div> </div>
</template> </template>
<script> <script>
import HelloWorld from './components/HelloWorld.vue' import HelloWorld from './components/HelloWorld.vue'
import Count from './components/Count.vue'
export default { export default {
name: 'App', name: 'App',
components: { components: {
HelloWorld HelloWorld,
Count
} }
} }
</script> </script>
...@@ -25,4 +28,8 @@ export default { ...@@ -25,4 +28,8 @@ export default {
color: #2c3e50; color: #2c3e50;
margin-top: 60px; margin-top: 60px;
} }
#hello {
display: none;
}
</style> </style>
<template>
<div>
<h1>当前求和为:{{ $store.getters.bigSum }}</h1>
<select v-model.number="n">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementOdd">当前求和为奇数再加</button>
<button @click="incrementWait">等一等再加</button>
</div>
</template>
<script>
export default {
name: "CountA",
data() {
return {
n: 1, //用户选择的数字
};
},
methods: {
increment() {
this.$store.commit("ADD", this.n);
},
decrement() {
this.$store.commit("SUBTRACT", this.n);
},
incrementOdd() {
this.$store.dispatch("addOdd", this.n);
},
incrementWait() {
this.$store.dispatch("addWait", this.n);
},
},
};
</script>
<style>
button {
margin-left: 5px;
}
</style>
...@@ -5,12 +5,37 @@ Vue.use(Vuex) ...@@ -5,12 +5,37 @@ Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
sum:0 //当前的和
}, },
getters: { getters: {
bigSum(state) {
console.log(state)
return state.sum * 10
}
}, },
mutations: { mutations: {
ADD(state,value){
console.log("mutations ADD")
state.sum += value
},
SUBTRACT(state,value){
console.log("mutations SUBTRACT")
state.sum -= value
}
}, },
actions: { actions: {
addOdd(context,value){
console.log("actions中的addOdd被调用了")
if(context.state.sum % 2){
context.commit('ADD',value)
}
},
addWait(context,value){
console.log("actions中的addWait被调用了")
setTimeout(()=>{
context.commit('ADD',value)
},500)
},
}, },
modules: { modules: {
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册