Vuex的actions屬性的具體使用
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 | 進(jìn)行中 |
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)來實(shí)現(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有兩點(diǎn)不同:
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: {} //用戶詳細(xì)資料數(shù)據(jù) }; const getters = { //獲取到用戶狀態(tài),//實(shí)時監(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 實(shí)例具有相同方法和屬性 // 頁面定義的setGargetUser,targetUser為頁面?zhèn)鬟^來的值 setGargetUser({ commit }, targetUser) { commit("SET_TARGET_USER", targetUser); } };
存儲頁面:
this.$store.dispatch('setGargetUser',friend)
獲取頁面:
computed:{ // 提示vuex中存入的用戶詳細(xì)資料 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 實(shí)現(xiàn)中是很常見的模式。同時把這些常量放在單獨(dú)的文件中可以讓協(xié)作開發(fā)變得清晰。 * 定義存儲信息 * * */ const types = { SET_TARGET_USER: "SET_TARGET_USER" //詳細(xì)資料 }; const state = { //用戶初始化的狀態(tài) targetUser: {} //用戶詳細(xì)資料數(shù)據(jù) }; const getters = { //獲取到用戶狀態(tài),//實(shí)時監(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 實(shí)例具有相同方法和屬性 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)文章
vue復(fù)制內(nèi)容到剪切板代碼實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于vue復(fù)制內(nèi)容到剪切板代碼實(shí)現(xiàn)的相關(guān)資料,在Web應(yīng)用程序中剪貼板(Clipboard)操作是非常常見的操作之一,需要的朋友可以參考下2023-08-08vue中LocalStorage與SessionStorage的區(qū)別與用法
本文主要介紹了LocalStorage和SessionStorage。LocalStorage和SessionStorage是兩種存儲方式,本文詳細(xì)的介紹一下區(qū)別以及用法,感興趣的可以了解一下2021-09-09Vuex處理用戶Token過期及優(yōu)化設(shè)置封裝本地存儲操作模塊
這篇文章主要為大家介紹了Vuex處理用戶Token優(yōu)化設(shè)置封裝本地存儲操作模塊及Token?過期問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09