这次给大家带来vuex+Actions使用详解,vuex+Actions使用的注意事项有哪些,下面就是实战案例,一起来看一下。
Action 提交的是 mutation,而不是直接变更状态. Action 是异步的,mutation是同步的。
沿用vuex学习---简介的案例:这里是加10 减1
1.在store.js 中 代码为:
import Vue from'vue'
import Vuex from'vuex'
//使用vuex模块
Vue.use(Vuex);
//声明静态常量为4
const state = {
count : 4
};
const mutations = {
add(state,n){
state.count +=n.a;
},
sub(state){
state.count--;
}
};
const actions = {
//2种书写方式
addplus(context){//可以理解为代表了整个的context
context.commit('add',{a:10})
},
subplus({commit}){
commit('sub');
}
};
//导出一个模块
exportdefaultnewVuex.Store({
state,
mutations,
actions
})2.在App.vue中 代码如下:
<template>
<p id="app">
<p id="appaaa">
<h1>这是vuex的示例</h1>
<p>组件内部count{{count}}</p>
<p>
<button @click ="addplus">+</button>
<button @click ="subplus">-</button>
</p>
</p>
</p>
</p>
</template>
<script>
//引入mapGetters
import {mapState,mapMutations,mapGetters,mapActions} from'vuex'
exportdefault{
name:'app',
data(){
return{
}
},
computed:{
...mapState([
"count"
]),
},
methods:{
...mapActions([
"addplus",
"subplus"
])
}
}
</script>
<style>
</style>相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
立即学习“前端免费学习笔记(深入)”;
以上就是vuex+Actions使用详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号