最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vuex的actions屬性的具體使用

 更新時間:2019年04月14日 16:46:31   作者:周家大小姐.  
這篇文章主要介紹了Vuex的actions屬性的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Vuex 的 action 屬性內(nèi),可以定義異步操作邏輯,以滿足某些業(yè)務(wù)場景要求。在組件內(nèi),是通過 $store.dispatch 來觸發(fā) action 定義的函數(shù)。

我們使用 action,來為計數(shù)器異步增 1。

1 Promise 方式

main.js:

const store = new Vuex.Store({
  state: {
    count: 0,
  },
  mutations: {
    increment(state, n = 1) {
      state.count += n;
    }
  },
  actions: {
    asyncInrement(context) {
      return new Promise(resolve => {
        setTimeout(() => {
          context.commit('increment');
          resolve();
        }, 1000)
      });
    }
  }
});

這里使用了 Promise ,在 1 s 后提交了 mutations 中定義的 increment 遞增函數(shù)。它是 ES6 語法,有三種狀態(tài):

狀態(tài) 說明
Pending 進行中
Resolved 已完成
Rejected 失敗

 index.vue:

<template>

  <div>
    {{count}}
    <button @click="asyncIncrementByAction">+1</button>
  </div>
</template>

<script>
  export default {
    name: "index.vue",
    computed: {
      count() {
        return this.$store.state.count;
      }
    },
    methods: {
      asyncIncrementByAction() {
        this.$store.dispatch('asyncInrement').then(() => {
          console.log(this.$store.state.count);
        })
      }
    }
  }
</script>

2 Callback 方式

也可以使用普通回調(diào)來實現(xiàn)異步方案。

main.js

const store = new Vuex.Store({
...
  actions: {
   ...
    asyncInrement2(context, callback) {
      setTimeout(() => {
        context.commit('increment');
        callback();
      }, 1000);
    }
  }
});

index.vue:

<template>
  <div>
    ...
    {{count}}
    <button @click="asyncIncrementByAction2">+1(Callback)</button>
  </div>
</template>

<script>
  export default {
    ...
    methods: {
      ...
      asyncIncrementByAction2() {
        this.$store.dispatch('asyncInrement2',() => {
          console.log(this.$store.state.count);
        });
      }
    }
  }
</script>

3 效果

vuex action和mutations的區(qū)別

action的功能和mutation是類似的,都是去變更store里的state,不過action和mutation有兩點不同:

1、action主要處理的是異步的操作,mutation必須同步執(zhí)行,而action就不受這樣的限制,也就是說action中我們既可以處理同步,也可以處理異步的操作

2、action改變狀態(tài),最后是通過提交mutation

使用方式: 

安裝:

npm install vuex --save

引用:

store.js

方法一:

/**
 * 創(chuàng)建完文件后需要去到main.js中引入成全局
 */
import Vue from "vue";
import Vuex from "vuex";
//使用vuex
Vue.use(Vuex);
const state = {
 targetUser: {} //用戶詳細資料數(shù)據(jù)
};
 
const getters = {
 //獲取到用戶狀態(tài),//實時監(jiān)聽state值的變化(最新狀態(tài))
 targetUser: state => state.targetUser
};
 
const mutations = {
 //自定義改變state初始值的方法
 SET_TARGET_USER(state, targetUser) {
  if (targetUser) {
   state.targetUser = targetUser; //如果targetUser有內(nèi)容就賦給狀態(tài)信息
  } else {
   //如果沒內(nèi)容就給targetUser賦空對象
   state.targetUser = {};
  }
 }
};
 
const actions = {
 //這里面的方法是用來異步觸發(fā)mutations里面的方法,context與store 實例具有相同方法和屬性
 // 頁面定義的setGargetUser,targetUser為頁面?zhèn)鬟^來的值
 setGargetUser({ commit }, targetUser) {
  commit("SET_TARGET_USER", targetUser);
 }
};

存儲頁面:

this.$store.dispatch('setGargetUser',friend)

獲取頁面:

 computed:{
    // 提示vuex中存入的用戶詳細資料
    targetUser(){
      return this.$store.getters.targetUser
    }
   },

以上方法有一個問題就是如果多人開發(fā);會出現(xiàn)不利于管理,下面用一個方法定義一個常量

存儲:

this.$store.dispatch('setUser',decode)

store.js

/**
 * 創(chuàng)建完文件后需要去到main.js中引入成全局
 */
import Vue from "vue";
import Vuex from "vuex";
// 持久存儲插件
import createPersistedState from "vuex-persistedstate";
 
//使用vuex
Vue.use(Vuex);
 
/**
 * 在需要多人協(xié)作的項目中,我們可以使用常量代替mutation 事件類型。這在各種 Flux 實現(xiàn)中是很常見的模式。同時把這些常量放在單獨的文件中可以讓協(xié)作開發(fā)變得清晰。
 * 定義存儲信息
 *
 *  */
 
const types = {
 SET_TARGET_USER: "SET_TARGET_USER" //詳細資料
};
 
const state = {
 //用戶初始化的狀態(tài)
 targetUser: {} //用戶詳細資料數(shù)據(jù)
};
const getters = {
 //獲取到用戶狀態(tài),//實時監(jiān)聽state值的變化(最新狀態(tài))
 targetUser: state => state.targetUser
};
const mutations = {
 //自定義改變state初始值的方法
 
 [types.SET_TARGET_USER](state, targetUser) {
  if (targetUser) {
   state.targetUser = targetUser; //如果targetUser有內(nèi)容就賦給狀態(tài)信息
  } else {
   //如果沒內(nèi)容就給targetUser賦空對象
   state.targetUser = {};
  }
 }
};
 
const actions = {
 //這里面的方法是用來異步觸發(fā)mutations里面的方法,context與store 實例具有相同方法和屬性
 setGargetUser({ commit }, targetUser) {
  commit(types.SET_TARGET_USER, targetUser);
  // localStorage.setItem("SET_TARGET_USER", JSON.stringify(targetUser));
 }
};
export default new Vuex.Store({
 state,
 mutations,
 getters,
 actions,
});

取值:

this.$store.getters.targetUser

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

皮山县| 昌都县| 越西县| 孟村| 德阳市| 桃园市| 蓝田县| 长寿区| 岢岚县| 东源县| 肥东县| 通道| 阿坝| 蒙城县| 合作市| 辽中县| 上栗县| 全州县| 保亭| 会宁县| 会同县| 金门县| 黔西县| 嘉黎县| 冕宁县| 辉南县| 台东县| 栾川县| 舟山市| 崇仁县| 江西省| 阿鲁科尔沁旗| 晴隆县| 商水县| 宕昌县| 明溪县| 皋兰县| 右玉县| 张家川| 嘉定区| 保定市|