Vuex中actions的使用教程詳解
簡介
說明
本文用示例介紹Vuex的五大核心之一:actions。
官網(wǎng)
actions概述
說明
Vuex 中的 mutation 非常類似于事件:每個 mutation 都有一個字符串的 事件類型 (type) 和 一個 回調函數(shù) (handler)。這個回調函數(shù)就是我們實際進行狀態(tài)更改的地方,并且它會接受 state 作為第一個參數(shù)。
特點
1.異步操作,通過mutations來改變state。
2.不能直接改變state里的數(shù)據(jù)。
3.包含多個事件回調函數(shù)的對象。
4.執(zhí)行方式:通過執(zhí)行 commit()來觸發(fā) mutation 的調用, 間接更新 state
5.觸發(fā)方式: 組件中: $store.dispatch(‘action 名稱’, data1)
6.可以包含異步代碼(例如:定時器, 請求后端接口)。
用法
直接使用
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>計數(shù)器的值:{{thisCount}}</div> <div>計數(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, } ], })
測試
訪問: http://localhost:8080/#/parent
到此這篇關于Vuex中actions的使用教程詳解的文章就介紹到這了,更多相關Vuex actions內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于vue的npm run dev和npm run build的區(qū)別介紹
這篇文章主要介紹了關于vue的npm run dev和npm run build的區(qū)別介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01