欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vuex中actions的使用教程詳解

 更新時間:2022年01月28日 09:29:52   作者:IT利刃出鞘  
actions作為Vuex的五大核心之一,它的屬性是用來處理異步方法的,通過提交mutations實現(xiàn)。本文將具體介紹一下actions的使用教程,需要的可以參考一下

簡介

說明

本文用示例介紹Vuex的五大核心之一:actions。

官網(wǎng)

Action | Vuex

API 參考 | Vuex

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?github用戶搜索案例分享

    Vue?github用戶搜索案例分享

    這篇文章主要介紹了Vue?github用戶搜索案例分享,文章基于Vue的相關資料展開對主題的詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • vue內置組件Transition的示例詳解

    vue內置組件Transition的示例詳解

    這篇文章主要介紹了vue內置組件Transition的詳解,簡單地說,就是當元素發(fā)生變化,比如消失、顯示時,添加動畫讓它更自然過渡,它是vue內置組件,不需要引入注冊就可以直接使用,本文通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • Vue3搭建組件庫開發(fā)環(huán)境的示例詳解

    Vue3搭建組件庫開發(fā)環(huán)境的示例詳解

    這篇文章給大家分享Vue3搭建組件庫開發(fā)環(huán)境,給大家講解依次搭建組件庫、example、文檔、cli,本文內容是搭建組件庫的開發(fā)環(huán)境的過程,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • vue?導入js中的兩種方法(示例詳解)

    vue?導入js中的兩種方法(示例詳解)

    這篇文章主要介紹了vue?導入js中的方法,本文通過兩種方法結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • vue儲存storage時含有布爾值的解決方案

    vue儲存storage時含有布爾值的解決方案

    這篇文章主要介紹了vue儲存storage時含有布爾值的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • vue3如何加載本地圖片等靜態(tài)資源淺析

    vue3如何加載本地圖片等靜態(tài)資源淺析

    在最近新起的項目中,用到了較新的技術棧vue3.2+vite+ts,跟著網(wǎng)上的寫法漸漸上手了,下面這篇文章主要給大家介紹了關于vue3如何加載本地圖片等靜態(tài)資源的相關資料,需要的朋友可以參考下
    2023-04-04
  • 詳解Vue 路由組件傳參的 8 種方式

    詳解Vue 路由組件傳參的 8 種方式

    這篇文章主要介紹了Vue 路由組件傳參的 8 種方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 關于vue的npm run dev和npm run build的區(qū)別介紹

    關于vue的npm run dev和npm run build的區(qū)別介紹

    這篇文章主要介紹了關于vue的npm run dev和npm run build的區(qū)別介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Vue項目中使用fontawesome圖標庫的方法

    Vue項目中使用fontawesome圖標庫的方法

    fontawesome的圖標有免費版和專業(yè)版,本文主要使用free版本,一般free版本的圖標夠用,free圖標又劃分為三個圖標庫,主要有實心圖標solid、常規(guī)圖標regular及品牌圖標brand,根據(jù)需求去下載對應的圖標庫,無須全部下載,對vue?fontawesome圖標庫相關知識感興趣的朋友一起看看吧
    2023-12-12
  • 解決vue-router路由攔截造成死循環(huán)問題

    解決vue-router路由攔截造成死循環(huán)問題

    這篇文章主要介紹了解決vue-router路由攔截造成死循環(huán)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08

最新評論