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

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

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

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 的模塊有多個(gè)格式是完全一樣的, 這時(shí)候就可以把這個(gè)模塊公共出來, 在 Vuex 實(shí)例里引用, 如:

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 卻需要每個(gè)單獨(dú)設(shè)置, 如果直接設(shè)置在lists里, 會(huì)導(dǎo)致 state 對(duì)象被引用共享

在 vuex@2.3.0 中, 這個(gè)問題將不存在

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

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

相關(guān)文章

  • 對(duì)vue事件的延遲執(zhí)行實(shí)例講解

    對(duì)vue事件的延遲執(zhí)行實(shí)例講解

    今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue中使用Datav如何完成大屏基本布局

    Vue中使用Datav如何完成大屏基本布局

    這篇文章主要介紹了Vue中使用Datav如何完成大屏基本布局問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • vue.js vue-router如何實(shí)現(xiàn)無效路由(404)的友好提示

    vue.js vue-router如何實(shí)現(xiàn)無效路由(404)的友好提示

    眾所周知vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁面應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue.js中vue-router如何實(shí)現(xiàn)無效路由(404)的友好提示的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • 詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題

    詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題

    這篇文章主要介紹了詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04
  • Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密的操作方法

    Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密的操作方法

    這篇文章主要介紹了Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密,在這里需要注意要加密的數(shù)據(jù)必須是字符串,對(duì)Vue?RSA非對(duì)稱加密相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-04-04
  • vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例

    vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例

    本文主要介紹了vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹

    Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹

    這篇文章主要介紹了Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • vue-router跳轉(zhuǎn)時(shí)打開新頁面的兩種方法

    vue-router跳轉(zhuǎn)時(shí)打開新頁面的兩種方法

    這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時(shí)打開新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-router具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Vue如何解決子組件data從props中無法動(dòng)態(tài)更新數(shù)據(jù)問題

    Vue如何解決子組件data從props中無法動(dòng)態(tài)更新數(shù)據(jù)問題

    這篇文章主要介紹了Vue如何解決子組件data從props中無法動(dòng)態(tài)更新數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 安裝VUE-CLI一直失敗的排錯(cuò)過程及解決方案

    安裝VUE-CLI一直失敗的排錯(cuò)過程及解決方案

    這篇文章主要介紹了安裝VUE-CLI一直失敗的排錯(cuò)過程及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評(píng)論

湖口县| 新竹市| 巴彦淖尔市| 桃园市| 红桥区| 漳浦县| 平邑县| 乌什县| 萨嘎县| 镇江市| 任丘市| 镇巴县| 理塘县| 玉山县| 宁津县| 丹东市| 和龙市| 孟村| 搜索| 仁寿县| 祁门县| 罗平县| 徐州市| 隆昌县| 柳林县| 长沙市| 鄂托克前旗| 咸阳市| 奉新县| 信丰县| 新津县| 龙游县| 饶阳县| 新乡市| 监利县| 日喀则市| 长岛县| 卢龙县| 甘孜县| 玛多县| 临城县|