這篇文章主要講解了Vuex的熱更替的實(shí)現(xiàn)方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
韶關(guān)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),韶關(guān)網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為韶關(guān)上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的韶關(guān)做網(wǎng)站的公司定做!
前言
我們在使用Vuex的時候,會時不時的更改Vuex內(nèi)的數(shù)據(jù),但是頁面不會隨之更新,如果數(shù)據(jù)量大,一個數(shù)據(jù)依賴另一個數(shù)據(jù)的話,這樣我們要是再刷新頁面的話會把以前依賴的數(shù)據(jù)清空,效率特別低。所以,今天我總結(jié)了怎么實(shí)現(xiàn)Vuex熱更替的功能。
實(shí)現(xiàn)
首先,我們這里使用了Vue CLI3。在根目錄下的src目錄下我們有一個存放Vuex的文件夾叫做store文件夾。首先我們分割成幾個模塊。
下面我們把它們分別引入,這里沒有分割actions,不過與其他屬性同理,這里有不做介紹。下面我們在index.js編輯下面代碼:
import Vuex from 'vuex' // 引入分割的模塊 import state from './state/state' import mutations from './mutations/mutations' import getters from './getters/getters' export default ()=>{ // 這里需要賦給一個store變量 const store = new Vuex.Store({ state:state, mutations:mutations, getters:getters }) // 熱更新模塊 if(module.hot){ // 跟上面一樣,寫入對應(yīng)的分割模塊路徑 module.hot.accept([ './state/state', './mutations/mutations', './getters/getters' ],()=>{ // 開啟熱更替 const newState = require('./state/state').default const newMutations = require('./mutations/mutations').default const newGetters = require('./getters/getters').default store.hotUpdate({ state:newState, mutations:newMutations, getters:newGetters }) }) } return store }
我們還需要在main.js修改:
import Vue from 'vue' import App from './App.vue' import Vuex from 'vuex' import createStore from './store/index.js' Vue.config.productionTip = false Vue.use(Vuex) const store=createStore(); new Vue({ store, render: h => h(App) }).$mount('#app')
一些其他api
// store.registerModule({ //動態(tài)添加模塊 // }) // 相當(dāng)于getter // store.watch((state)=>state.count+1,(newCount)=>{ // console.log('new count watched , '+newCount) // }) // mutation被調(diào)用時 // store.subscribe((mutation,state)=>{ // console.log(mutation.type) // console.log(mutation.payload) // }) // action被調(diào)用時 // store.subscribeAction((action,state)=>{ // console.log(action.type) // console.log(action.payload) // })
看完上述內(nèi)容,是不是對Vuex的熱更替的實(shí)現(xiàn)方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:Vuex的熱更替的實(shí)現(xiàn)方法
網(wǎng)頁URL:http://bm7419.com/article18/goeigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站設(shè)計公司、品牌網(wǎng)站制作、軟件開發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)