日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何實(shí)現(xiàn)Vuex的熱更新

前言

我們?cè)谑褂肰uex的時(shí)候,會(huì)時(shí)不時(shí)的更改Vuex內(nèi)的數(shù)據(jù),但是頁(yè)面不會(huì)隨之更新,如果數(shù)據(jù)量大,一個(gè)數(shù)據(jù)依賴另一個(gè)數(shù)據(jù)的話,這樣我們要是再刷新頁(yè)面的話會(huì)把以前依賴的數(shù)據(jù)清空,效率特別低。所以,今天我總結(jié)了怎么實(shí)現(xiàn)Vuex熱更替的功能。

實(shí)現(xiàn)

首先,我們這里使用了Vue CLI3。在根目錄下的src目錄下我們有一個(gè)存放Vuex的文件夾叫做store文件夾。首先我們分割成幾個(gè)模塊。

下面我們把它們分別引入,這里沒(méi)有分割actions,不過(guò)與其他屬性同理,這里有不做介紹。下面我們?cè)趇ndex.js編輯下面代碼:

 
 
 
 
  1. import Vuex from 'vuex' 
  2. // 引入分割的模塊 
  3. import state from './state/state' 
  4. import mutations from './mutations/mutations' 
  5. import getters from './getters/getters' 
  6.  
  7. export default ()=>{ 
  8. // 這里需要賦給一個(gè)store變量 
  9.  const store = new Vuex.Store({ 
  10.     state:state, 
  11.     mutations:mutations, 
  12.     getters:getters 
  13.   }) 
  14.   // 熱更新模塊 
  15.   if(module.hot){ 
  16.   // 跟上面一樣,寫入對(duì)應(yīng)的分割模塊路徑 
  17.     module.hot.accept([ 
  18.       './state/state', 
  19.       './mutations/mutations', 
  20.       './getters/getters' 
  21.     ],()=>{ 
  22.     // 開(kāi)啟熱更替 
  23.       const newState = require('./state/state').default 
  24.       const newMutations = require('./mutations/mutations').default 
  25.       const newGetters = require('./getters/getters').default 
  26.       store.hotUpdate({ 
  27.         state:newState, 
  28.         mutations:newMutations, 
  29.         getters:newGetters 
  30.       }) 
  31.     }) 
  32.   } 
  33.    
  34.   return store 

我們還需要在main.js修改:

 
 
 
 
  1. import Vue from 'vue' 
  2. import App from './App.vue' 
  3. import Vuex from 'vuex' 
  4. import createStore from './store/index.js' 
  5.  
  6. Vue.config.productionTip = false 
  7.  
  8. Vue.use(Vuex) 
  9. const store=createStore(); 
  10.  
  11. new Vue({ 
  12.   store, 
  13.   render: h => h(App) 
  14. }).$mount('#app') 

結(jié)語(yǔ)

以上,就完成了Vuex的熱更替功能。需要注意的是,直接在state中更改是看不到效果的哦!謝謝閱讀。


標(biāo)題名稱:如何實(shí)現(xiàn)Vuex的熱更新
標(biāo)題鏈接:http://www.dlmjj.cn/article/dpiodpi.html