欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Vuex@2.3.0 中的 state 支持函數(shù)申明

 更新時間:2017年11月22日 10:58:10   作者:M.M.F  
這篇文章主要介紹了淺談Vuex@2.3.0 中的 state 支持函數(shù)申明,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

vuex 2.3.0 的發(fā)布說明: 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)

假如你 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 } }},
      }
    }
})

但是 state 卻需要每個單獨設置, 如果直接設置在lists里, 會導致 state 對象被引用共享

在 vuex@2.3.0 中, 這個問題將不存在

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,
      }
    }
})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue中播放rtsp流的方法實例詳解

    vue中播放rtsp流的方法實例詳解

    最近有個需求是前端在瀏覽器顯示攝像頭傳回的RTSP視頻流,下面這篇文章主要給大家介紹了關于vue中播放rtsp流的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Element-Ui組件 NavMenu 導航菜單的具體使用

    Element-Ui組件 NavMenu 導航菜單的具體使用

    這篇文章主要介紹了Element-Ui組件 NavMenu 導航菜單的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • vue3中watch與watchEffect的區(qū)別

    vue3中watch與watchEffect的區(qū)別

    vue3 新增的 Composition API中的 watchEffect 和 watch都可在 setup() 函數(shù)中使用,本文重點介紹vue3中watch與watchEffect的區(qū)別,感興趣的朋友一起看看吧
    2023-02-02
  • vue3+vite+axios?配置連接后端調用接口的實現(xiàn)方法

    vue3+vite+axios?配置連接后端調用接口的實現(xiàn)方法

    這篇文章主要介紹了vue3+vite+axios?配置連接后端調用接口的實現(xiàn)方法,在vite.config.ts文件中添加配置,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • vue-cli開發(fā)環(huán)境實現(xiàn)跨域請求的方法

    vue-cli開發(fā)環(huán)境實現(xiàn)跨域請求的方法

    本篇文章主要介紹了vue-cli開發(fā)環(huán)境實現(xiàn)跨域請求的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 淺談VueUse中useAsyncState的實現(xiàn)原理

    淺談VueUse中useAsyncState的實現(xiàn)原理

    useAsyncState?是 VueUse 庫中提供的一個實用工具,它用于處理異步狀態(tài),本文主要介紹了VueUse中useAsyncState的實現(xiàn)及其原理,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • 理順8個版本vue的區(qū)別(小結)

    理順8個版本vue的區(qū)別(小結)

    這篇文章主要介紹了理順8個版本vue的區(qū)別(小結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue實現(xiàn)物流時間軸效果

    vue實現(xiàn)物流時間軸效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)物流時間軸效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue截圖轉base64轉文件File異步獲取方式

    vue截圖轉base64轉文件File異步獲取方式

    這篇文章主要介紹了vue截圖轉base64轉文件File異步獲取方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue-treeselect無法點擊問題(點擊無法出現(xiàn)拉下菜單)

    vue-treeselect無法點擊問題(點擊無法出現(xiàn)拉下菜單)

    這篇文章主要介紹了vue-treeselect無法點擊問題(點擊無法出現(xiàn)拉下菜單),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評論