国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue核心概念A(yù)ction的總結(jié)

2019-11-19 12:15:44
字體:
供稿:網(wǎng)友

Action 類似于mutation,不同在于:

- Action 提交的是mutation,而不是直接變更狀態(tài)。

- Action 可以包含任意異步操作。

Action 函數(shù)接受一個與 store 實例具有相同方法和屬性的 context 對象,因此你可以調(diào)用 context.commit 提交一個 mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。當我們在之后介紹到 Modules 時,你就知道 context 對象為什么不是 store 實例本身了。

代碼實例

store.js

  actions: {    // 商品半價    disCount (content) {      // actions中通過commit()提交mutations中的goodsPriceHalve()函數(shù)      content.commit('goodsPriceHalve')    },    // 異步的商品半價    /**     * disCountAsync ({commit}){      setTimeout(() => {        commit('goodsPriceHalve')      },1000)    } */    disCountAsync ({commit}){      setTimeout(() => {        commit('goodsPriceHalve')      },1000)    },    // 統(tǒng)一修改商品名字    /* acyionChangeName()的第一個參數(shù)可以理解成是一個對象,第二個參數(shù)為事件傳遞過來的參數(shù)    這個參數(shù)也需要傳遞給mutations中相對應(yīng)的函數(shù)中*/    acyionChangeName(content,payload){      // 載荷形式      content.commit('changeName',payload);    },    acyionChangeNameAsync (content,payload){      setTimeout(() => {        // 對象形式        content.commit('changeName',payload.payload)      },1000)    }  }

這里有同步也有異步,有載荷也有沒有載荷。

page7.vue

<template>  <div>    <h2>我是第五個頁面</h2>    <div>      <input type="text" placeholder="請輸入商品的新名字" v-model="inpValue">      <button @click="disCountPrice()">同步商品價格減半</button>      <button @click="disCountPriceAsync()">異步商品價格減半</button>    </div>    <ul class="ul_list">      <li v-for="item in goodsListHalv">        <p class="name">商品:{{item.name}}</p>        <p class="price">價格:¥{{item.price}}</p>      </li>    </ul>  </div></template><script> export default {   data() {    return {      inpValue:'',    }   },   computed: {     goodsListHalv(){       return this.$store.state.goodsList;     }   },   methods: {    //  注意這里使用箭頭函數(shù)可能會報錯    // 同步商品價格減半;觸發(fā)事件提交actions中的disCount()函數(shù)     disCountPrice: function(){       this.$store.dispatch('disCount')     },     // 異步商品價格減半     disCountPriceAsync: function(){       this.$store.dispatch('disCountAsync')     },   }  }</script>

page8.vue

<template>  <div>    <h2>我是第六個頁面</h2>    <div>      <input type="text" placeholder="請輸入商品的新名字" v-model="inpValue">      <button @click="acyionChangeName()">載荷修改商品的名字(字符串)</button>      <button @click="acyionChangeNameAsync()">載荷修改商品的名字(對象)</button>    </div>    <ul class="ul_list">      <li v-for="item in goodsListHalv" :key="item.id">        <p class="name">商品:{{item.name}}</p>        <p class="price">價格:¥{{item.price}}</p>      </li>    </ul>  </div></template><script> export default {   data() {    return {      inpValue:'',    }   },   computed: {     goodsListHalv(){       return this.$store.state.goodsList;     }   },   methods: {    //  注意這里使用箭頭函數(shù)可能會報錯    // 同步修改商品的名字;觸發(fā)事件提交actions中的acyionChangeName()函數(shù)     acyionChangeName: function(){       this.$store.dispatch('acyionChangeName',this.inpValue)     },     // 異步修改商品的名字;對象 對象中包含兩個屬性type事件函數(shù),payload參數(shù)     acyionChangeNameAsync: function(){       this.$store.dispatch({         type:'acyionChangeNameAsync',         payload:this.inpValue       })     },   }  }</script>

乍一眼看上去感覺多此一舉,我們直接分發(fā) mutation 豈不更方便?實際上并非如此,還記得 mutation 必須同步執(zhí)行這個限制么?Action 就不受約束!我們可以在 action 內(nèi)部執(zhí)行異步操作。

Actions 支持同樣的載荷方式和對象方式進行分發(fā)。

效果圖

代碼執(zhí)行過程:

上面的Action執(zhí)行過程是:按鈕點擊

主站蜘蛛池模板: 英山县| 濮阳县| 资源县| 黑河市| 冷水江市| 商洛市| 屯留县| 博乐市| 纳雍县| 福安市| 兴安盟| 塔河县| 琼海市| 玛多县| 涞水县| 阜城县| 宝清县| 辉县市| 庆安县| 台安县| 株洲县| 浦县| 乌海市| 江永县| 彰武县| 婺源县| 沙田区| 蚌埠市| 彭泽县| 剑阁县| 伊金霍洛旗| 城市| 许昌县| 乐清市| 长沙县| 金山区| 边坝县| 漳浦县| 庆安县| 来宾市| 长治市|