關於Vuex@2.3.0 中的 state 支援函數的申明
這篇文章主要介紹了淺談Vuex@2.3.0 中的 state 支援函數申明,內容蠻不錯的,現在分享給大家,也給大家做個參考。
vuex 2.3.0 的發布說明: Modules can now declare state using a function - this allows the same module definition to be reused (e.g. multiple times in the same store, or in multiple# stores in multiple#s) in the same store, or in multiple# stores) in multiple#s) in ##假如你vuex 的模組有多個格式是完全一樣的, 這時候就可以把這個模組公共出來, 在Vuex 實例裡引用, 如:
##
import api from '~api' const actions = { async ['get']({commit, rootState: {route: { path }}}, config = {}) { const { data: { code, data } } = await api.post(config.url, config.data) if (code === 1001) commit('receive', data) } } const mutations = { ['receive'](state, data) { state.data = [].concat(data) }, ['modify'](state, payload) { const index = state.data.findIndex(item => item.id === payload.id) if (index > -1) { state.data.splice(index, 1, payload) } }, ['insert'](state, payload) { state.data = [payload].concat(state.data) }, ['remove'](state, id) { const index = state.data.findIndex(item => item.id === id) state.data.splice(index, 1) } } const getters = { ['get'](state) { return state.data } } export const _actions = actions export const _mutations = mutations export const _getters = getters export default { namespaced: true, actions, mutations, getters }
#
import Vue from 'vue' import Vuex from 'vuex' import lists from './general/lists' Vue.use(Vuex) export default new Vuex.Store({ namespaced: true, modules: { base: { namespaced: true, modules: { app: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }}, platform: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }}, product: { namespaced: true, modules: { category: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }}, } }, keyword: { namespaced: true, modules: { username: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }}, } }, } }, buzz: { namespaced: true, modules: { shop: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }}, } } })
import api from '~api' const actions = { async ['get']({commit, rootState: {route: { path }}}, config = {}) { const { data: { code, data } } = await api.post(config.url, config.data) if (code === 1001) commit('receive', data) } } const mutations = { ['receive'](state, data) { state.data = [].concat(data) }, ['modify'](state, payload) { const index = state.data.findIndex(item => item.id === payload.id) if (index > -1) { state.data.splice(index, 1, payload) } }, ['insert'](state, payload) { state.data = [payload].concat(state.data) }, ['remove'](state, id) { const index = state.data.findIndex(item => item.id === id) state.data.splice(index, 1) } } const getters = { ['get'](state) { return state.data } } export const _actions = actions export const _mutations = mutations export const _getters = getters export default { namespaced: true, state() { return { lists: { data: [], total: 0, current_page: 1 } } }, actions, mutations, getters }
import Vue from 'vue' import Vuex from 'vuex' import lists from './general/lists' Vue.use(Vuex) export default new Vuex.Store({ namespaced: true, modules: { base: { namespaced: true, modules: { app: lists, platform: lists, product: { namespaced: true, modules: { category: lists, } }, keyword: { namespaced: true, modules: { username: lists, } }, } }, buzz: { namespaced: true, modules: { shop: lists, } } })
VUEJS 2.0 子元件存取/呼叫父元件
以上是關於Vuex@2.3.0 中的 state 支援函數的申明的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)
![在Vue應用程式中使用vuex時出現「Error: [vuex] unknown action type: xxx」怎麼解決?](https://img.php.cn/upload/article/000/887/227/168766615217161.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue.js專案中,vuex是一個非常有用的狀態管理工具。它可以幫助我們在多個元件之間共享狀態,並提供了一種可靠的方式來管理狀態的變化。但使用vuex時,有時會遇到「Error:[vuex]unknownactiontype:xxx」的錯誤。這篇文章將介紹該錯誤的原因及解決方法。 1.錯誤原因在使用vuex時,我們需要定義一些actions和mu
![在Vue應用中使用vuex時出現「Error: [vuex] do not mutate vuex store state outside mutation handlers.」怎麼解決?](https://img.php.cn/upload/article/000/000/164/168760467048976.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue應用程式中,使用vuex是常見的狀態管理方式。然而,在使用vuex時,我們有時可能會遇到這樣的錯誤提示:「Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers.」這個錯誤提示是什麼意思呢?為什麼會出現這個錯誤提示?如何解決這個錯誤?本文將詳細介紹這個問題。錯誤提示的含

Vue2.x是目前最受歡迎的前端框架之一,它提供了Vuex作為管理全域狀態的解決方案。使用Vuex能夠使得狀態管理更加清晰、易於維護,以下將介紹Vuex的最佳實踐,幫助開發者更好地使用Vuex以及提高程式碼品質。 1.使用模組化組織狀態Vuex使用單一狀態樹管理應用程式的全部狀態,將狀態從元件中抽離出來,使得狀態管理更加清晰易懂。在具有較多狀態的應用中,必須使用模組

Vuex是做什麼的? Vue官方:狀態管理工具狀態管理是什麼?需要在多個元件中共享的狀態、且是響應式的、一個變,全都改變。例如一些全域要用的狀態資訊:使用者登入狀態、使用者名稱、地理位置資訊、購物車中商品、等等這時候我們就需要這麼一個工具來進行全域的狀態管理,Vuex就是這樣的一個工具。單一頁面的狀態管理View–>Actions—>State視圖層(view)觸發操作(action)變更狀態(state)回應回視圖層(view)vuex(Vue3.

在Vue應用中使用Vuex是非常常見的操作。然而,偶爾在使用Vuex時會遇到錯誤訊息“TypeError:Cannotreadproperty'xxx'ofundefined”,這個錯誤訊息的意思是無法讀取undefined的屬性“xxx”,導致了程式的錯誤。這個問題其實產生的原因很明顯,就是因為在呼叫Vuex的某個屬性的時候,這個屬性沒有被正確

具體步驟:1、安裝vuex(vue3建議4.0+)pnpmivuex-S2、main.js中設定importstorefrom'@/store'//hx-app的全域設定constapp=createApp(App)app.use(store)3、新建相關的資料夾與文件,這裡配置多個不同vuex內部的js,使用vuex的modules來放置不同的頁面,文件,然後統一使用一個getters.jsindex.js核心文件,這裡使用了import.meta.glob ,而不

這篇文章帶大家聊聊Vue狀態管理,介紹一下兩個Vue狀態管理函式庫:Pinia和Vuex,希望對大家有幫助!
