web前端全系列 教程
1839个小节阅读:2243.8k
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Action 类似于 mutation,不同在于:
xxxxxxxxxx
import { createStore } from 'vuex'
const store = createStore({
state:{
count: 10
},
getters: {
getCount(state) {
return "当前Count值为: "+state.count;
}
},
mutations:{
increment(state,num){
state.count += Number(num)
},
decrement(state,num){
state.count -= Number(num)
}
},
actions:{
asyncIncrement(context,num){
setTimeout(() =>{
context.commit("increment",num)
},2000)
},
asyncDecrement(context,num){
context.commit("decrement",num)
}
}
})
export default store
xxxxxxxxxx
<template>
<h3>Nums</h3>
<p></p>
<input type="text" v-model="num">
<button @click="addHandler">增加</button>
<button @click="minHandler">减少</button>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
data(){
return{
num:""
}
},
computed:{
mapGetters(["getCount"])
},
methods:{
addHandler(){
this.$store.dispatch("asyncIncrement",this.num)
},
minHandler(){
this.$store.dispatch("asyncDecrement",this.num)
}
}
}
</script>
xxxxxxxxxx
<template>
<h3>Count</h3>
<p></p>
<input type="text" v-model="num">
<button @click="addHandler">增加</button>
<button @click="minHandler">减少</button>
</template>
<script setup>
import { computed,ref } from 'vue'
import { useStore } from 'vuex'
const num = ref("")
const store = useStore()
const currentCount = computed(() => {
return store.getters.getCount
})
function addHandler(){
store.dispatch("asyncIncrement",num.value)
}
function minHandler(){
store.dispatch("asyncDecrement",num.value)
}
</script>
实时效果反馈
1. 下列关于Vuex中的Action的描述正确的是:
A Action 提交的是 mutation,直接变更状态,可以包含任意异步操作
B Action 提交的是 mutation,直接变更状态
C Action 提交的是 mutation,而不是直接变更状态,必须是同步操作
D Action 提交的是 mutation,而不是直接变更状态,可以包含任意异步操作
答案
1=>D