Vuex中actions的使用教程詳解
簡(jiǎn)介
說明
本文用示例介紹Vuex的五大核心之一:actions。
官網(wǎng)
actions概述
說明
Vuex 中的 mutation 非常類似于事件:每個(gè) mutation 都有一個(gè)字符串的 事件類型 (type) 和 一個(gè) 回調(diào)函數(shù) (handler)。這個(gè)回調(diào)函數(shù)就是我們實(shí)際進(jìn)行狀態(tài)更改的地方,并且它會(huì)接受 state 作為第一個(gè)參數(shù)。
特點(diǎn)
1.異步操作,通過mutations來改變state。
2.不能直接改變state里的數(shù)據(jù)。
3.包含多個(gè)事件回調(diào)函數(shù)的對(duì)象。
4.執(zhí)行方式:通過執(zhí)行 commit()來觸發(fā) mutation 的調(diào)用, 間接更新 state
5.觸發(fā)方式: 組件中: $store.dispatch(‘action 名稱’, data1)
6.可以包含異步代碼(例如:定時(shí)器, 請(qǐng)求后端接口)。
用法
直接使用
this.$store.dispatch('actions方法名', 具體值) // 不分模塊
this.$store.dispatch('模塊名/actions方法名', 具體值) // 分模塊
mapActions
import { mapActions } from 'vuex'
export default {
computed: {
// 不分模塊
...mapActions(['actions方法名'])
// 分模塊,不改方法名
...mapActions('模塊名', ['actions方法名'])
// 分模塊,不改方法名
...mapActions('模塊名',{'新actions方法名': '舊actions方法名'})
}
}
示例
CounterStore.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const counterStore = new Vuex.Store(
{
state: {
count: 10
},
getters: {
doubleCount(state) {
return state.count * 2;
}
},
mutations: {
increment(state) {
state.count++;
},
decrement(state) {
state.count--;
},
// 帶參數(shù)
addNumber(state, param1) {
state.count += param1;
},
},
actions: {
asyncIncrement(context) {
console.log('CounterStore=> action: asyncIncrement');
setTimeout(() => {context.commit('increment')}, 1000)
},
asyncAddNumber(context, n) {
console.log('CounterStore=> action: asyncAddNumber');
setTimeout(() => {context.commit('addNumber', n)}, 1000)
}
}
}
);
export default counterStore;
Parent.vue(入口組件)
<template>
<div class="outer">
<h3>父組件</h3>
<component-a></component-a>
<component-b></component-b>
</div>
</template>
<script>
import ComponentA from "./ComponentA";
import ComponentB from "./ComponentB";
export default {
name: 'Parent',
components: {ComponentA, ComponentB},
}
</script>
<style scoped>
.outer {
margin: 20px;
border: 2px solid red;
padding: 20px;
}
</style>
ComponentA.vue(異步修改vuex的數(shù)據(jù))
<template>
<div class="container">
<h3>ComponentA</h3>
<button @click="thisAsyncIncrement">異步加1</button>
<button @click="thisAsyncAddNumber">異步增加指定的數(shù)</button>
</div>
</template>
<script>
export default {
data() {
return {
cnt: 5
}
},
methods:{
thisAsyncIncrement() {
this.$store.dispatch('asyncIncrement')
},
thisAsyncAddNumber() {
this.$store.dispatch('asyncAddNumber', this.cnt)
}
}
}
</script>
<style scoped>
.container {
margin: 20px;
border: 2px solid blue;
padding: 20px;
}
</style>
ComponentB.vue(讀取vuex的數(shù)據(jù))
<template>
<div class="container">
<h3>ComponentB</h3>
<div>計(jì)數(shù)器的值:{{thisCount}}</div>
<div>計(jì)數(shù)器的2倍:{{thisDoubleCount}}</div>
</div>
</template>
<script>
export default {
computed:{
thisCount() {
return this.$store.state.count;
},
thisDoubleCount() {
return this.$store.getters.doubleCount;
},
}
}
</script>
<style scoped>
.container {
margin: 20px;
border: 2px solid blue;
padding: 20px;
}
</style>
路由(router/index.js)
import Vue from 'vue'
import Router from 'vue-router'
import Parent from "../components/Parent";
Vue.use(Router)
export default new Router({
routes: [
{
path: '/parent',
name: 'Parent',
component: Parent,
}
],
})
測(cè)試
訪問: http://localhost:8080/#/parent

到此這篇關(guān)于Vuex中actions的使用教程詳解的文章就介紹到這了,更多相關(guān)Vuex actions內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析vue-router中params和query的區(qū)別
這篇文章主要介紹了vue-router中params和query的區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue實(shí)現(xiàn)Input輸入框模糊查詢方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)Input輸入框模糊查詢方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue中element 的upload組件發(fā)送請(qǐng)求給后端操作
這篇文章主要介紹了vue中element 的upload組件發(fā)送請(qǐng)求給后端操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
解決ant design vue 表格a-table二次封裝,slots渲染的問題
這篇文章主要介紹了解決ant design vue 表格a-table二次封裝,slots渲染的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
淺談vue同一頁面中擁有兩個(gè)表單時(shí),的驗(yàn)證問題
今天小編就為大家分享一篇淺談vue同一頁面中擁有兩個(gè)表單時(shí),的驗(yàn)證問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue使用WEB自帶TTS實(shí)現(xiàn)語音文字互轉(zhuǎn)的操作方法
這篇文章主要介紹了vue使用WEB自帶TTS實(shí)現(xiàn)語音文字互轉(zhuǎn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
ElementUI Tree 樹形控件的使用并給節(jié)點(diǎn)添加圖標(biāo)
這篇文章主要介紹了ElementUI Tree 樹形控件的使用并給節(jié)點(diǎn)添加圖標(biāo),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Vue中接收二進(jìn)制文件流實(shí)現(xiàn)pdf預(yù)覽的方法
本文主要介紹了Vue中接收二進(jìn)制文件流實(shí)現(xiàn)pdf預(yù)覽的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

