是否可以從導(dǎo)入到組件中的單獨模塊調(diào)用 Vuex mapActions?我正在嘗試標準化 vue.js Web 應(yīng)用程序中的一組函數(shù)。我想將它們導(dǎo)入到每個組件中并傳遞一些值到函數(shù)操作。我正在使用 vuex 來管理狀態(tài)。目前,每個組件每次加載時都會調(diào)用這些函數(shù)(完全相同)。我想將其重構(gòu)為一個模塊,并根據(jù)需要將其導(dǎo)入到每個組件中。此代碼使用 mapActions 作為其功能的一部分。下面是相關(guān)的代碼段:component、module、vuex actionVue 組件://the imported function callif (!this.queued){ timer.updatePage(this.pagination, this.orders);}模塊代碼(advance.js):import { mapActions } from 'vuex';let currentComp = { name: 'purchase', date: null, start: false}const timer = { ...mapActions(['currentComponent']), updatePage(pagination, order) { currentComp.name = 'nextComponent'; this.currentComponent(currentComp); }}export default timer;Vuex 代碼://in the actions section:currentComponent({ commit }, comp) { console.log(comp); commit('setCurrentComponent', comp);}//in the mutations section:setCurrentComponent: (state, comp) => { state.currentComponent = comp.name; return state; }當組件運行導(dǎo)入的函數(shù)時,我得到:vuex.esm.js?2f62:870 Uncaught TypeError: Cannot read property 'dispatch' of undefined at Object.mappedAction [as currentComponent] (vuex.esm.js?2f62:870) at eval (advance.js?935c:37)當我從 this.currentComponent 中刪除 this 時,我得到:advance.js?935c:37 Uncaught ReferenceError: currentComponent is not defined at eval (advance.js?935c:37)提前感謝您的任何指導(dǎo)。
是否可以在導(dǎo)出的模塊中使用 Vuex mapActions
慕勒3428872
2021-06-29 08:41:42