Vuex的各個(gè)模塊封裝的實(shí)現(xiàn)
一、各個(gè)模塊的作用:
- state 用來(lái)數(shù)據(jù)共享數(shù)據(jù)存儲(chǔ)
- mutation 用來(lái)注冊(cè)改變數(shù)據(jù)狀態(tài)(同步)
- getters 用來(lái)對(duì)共享數(shù)據(jù)進(jìn)行過(guò)濾并計(jì)數(shù)操作
- action 解決異步改變共享數(shù)據(jù)(異步)
二、 創(chuàng)建文件:
- actions.js
- getters.js
- index.js
- mutations.js
- mutation-types.js
- state.js
三、編輯文件
這里只是拿出自己的項(xiàng)目來(lái)做一個(gè)例子,只是介紹封裝的方法。
index.js
import Vue from 'vue' import Vuex from 'vuex' import * as actions from './actions' import * as getters from './getters' import state from './state' import mutations from './mutations' import createLogger from 'vuex/dist/logger' // vuex調(diào)試工具 Vue.use(Vuex) const debug = process.env.NODE_ENV !== 'prodycution' // 開(kāi)發(fā)環(huán)境下開(kāi)啟嚴(yán)格模式 export default new Vuex.Store({ actions, getters, state, mutations, strict: debug, plugins: debug ? [createLogger()] : [] })
state.js
import {playMode} from 'common/js/config' import {loadSearch} from 'common/js/cache' const state = { singer: {}, playing: false, fullScreen: false, playlist: [], sequenceList: [], mode: playMode.sequence, currentIndex: -1, disc: {}, topList: {}, searchHistory: loadSearch() } export default state
mutations.js
import * as types from './mutation-types' const mutations = { [types.SET_SINGER](state, singer) { state.singer = singer }, [types.SET_PLAYING_STATE](state, flag) { state.playing = flag }, [types.SET_FULL_SCREEN](state, flag) { state.fullScreen = flag }, [types.SET_PLAYLIST](state, list) { state.playlist = list }, [types.SET_SEQUENCE_LIST](state, list) { state.sequenceList = list }, [types.SET_PLAY_MODE](state, mode) { state.mode = mode }, [types.SET_CURRENT_INDEX](state, index) { state.currentIndex = index }, [types.SET_DISC](state, disc) { state.disc = disc }, [types.SET_TOP_LIST](state, topList) { state.topList = topList }, [types.SET_SEARCH_HISTORY](state, history) { state.searchHistory = history } } export default mutations
mutation-types.js
export const SET_SINGER = 'SET_SINGER' export const SET_PLAYING_STATE = 'SET_PLAYING_STATE' export const SET_FULL_SCREEN = 'SET_FULL_SCREEN' export const SET_PLAYLIST = 'SET_PLAYLIST' export const SET_SEQUENCE_LIST = 'SET_SEQUENCE_LIST' export const SET_PLAY_MODE = 'SET_PLAY_MODE' export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX' export const SET_DISC = 'SET_DISC' export const SET_TOP_LIST = 'SET_TOP_LIST' export const SET_SEARCH_HISTORY = 'SET_SEARCH_HISTORY'
getters.js
export const singer = state => state.singer export const playing = state => state.playing export const fullScreen = state => state.fullScreen export const playlist = state => state.playlist export const sequenceList = state => state.sequenceList export const mode = state => state.mode export const currentIndex = state => state.currentIndex export const currentSong = (state) => { return state.playlist[state.currentIndex] || {} } export const disc = state => state.disc export const topList = state => state.topList export const searchHistory = state => state.searchHistory
actions.js
import * as types from './mutation-types' import {playMode} from 'common/js/config' import {shuffle} from 'common/js/util' import {saveSearch, deleteSearch, clearSearch} from 'common/js/cache' function findIndex(list, song) { return list.findIndex((item) => { return item.id === song.id }) } export const selectPlay = function ({commit, state}, {list, index}) { commit(types.SET_SEQUENCE_LIST, list) if (state.mode === playMode.random) { let randomList = shuffle(list) commit(types.SET_PLAYLIST, randomList) index = findIndex(randomList, list[index]) } else { commit(types.SET_PLAYLIST, list) } commit(types.SET_CURRENT_INDEX, index) commit(types.SET_FULL_SCREEN, true) commit(types.SET_PLAYING_STATE, true) } export const randomPlay = function({commit}, {list}) { commit(types.SET_PLAY_MODE, playMode.random) commit(types.SET_SEQUENCE_LIST, list) let randomList = shuffle(list) commit(types.SET_PLAYLIST, randomList) commit(types.SET_CURRENT_INDEX, 0) commit(types.SET_FULL_SCREEN, true) commit(types.SET_PLAYING_STATE, true) } export const insertSong = function({commit, state}, song) { let playlist = state.playlist.slice() let sequenceList = state.sequenceList.slice() let currentIndex = state.currentIndex // 記錄當(dāng)前歌曲 let currentSong = playlist[currentIndex] // 查找當(dāng)前列表中是否有待插入的歌曲并返回其索引 let fpIndex = findIndex(playlist, song) // 因?yàn)槭遣迦敫枨?,所以索引?1 currentIndex++ // 插入這首歌到當(dāng)前索引位置 playlist.splice(currentIndex, 0, song) // 如果已經(jīng)包含這首歌 if (fpIndex > -1) { // 如果當(dāng)前插入的序號(hào)大于列表中的序號(hào) if (currentIndex > fpIndex) { playlist.splice(fpIndex, 1) currentIndex-- } else { playlist.splice(fpIndex + 1, 1) } } let currentSIndex = findIndex(sequenceList, currentSong) + 1 let fsIndex = findIndex(sequenceList, song) sequenceList.splice(currentSIndex, 0, song) if (fsIndex > -1) { if (currentSIndex > fsIndex) { sequenceList.splice(fsIndex, 1) } else { sequenceList.splice(fsIndex + 1, 1) } } commit(types.SET_PLAYLIST, playlist) commit(types.SET_SEQUENCE_LIST, sequenceList) commit(types.SET_CURRENT_INDEX, currentIndex) commit(types.SET_FULL_SCREEN, true) commit(types.SET_PLAYING_STATE, true) } export const saveSearchHistory = function({commit}, query) { commit(types.SET_SEARCH_HISTORY, saveSearch(query)) } export const deleteSearchHistory = function({commit}, query) { commit(types.SET_SEARCH_HISTORY, deleteSearch(query)) } export const clearSeachHistory = function({commit}) { commit(types.SET_SEARCH_HISTORY, clearSearch()) }
小貼士:
默認(rèn)接口: export default
具名接口: export
1、export導(dǎo)出多個(gè)對(duì)象,export default只能導(dǎo)出一個(gè)對(duì)象。
2、export導(dǎo)出對(duì)象需要用{ },export default不需要{ }。
3、在其他文件引用export default導(dǎo)出的對(duì)象時(shí)不一定使用導(dǎo)出時(shí)的名字。因?yàn)檫@種方式實(shí)際上是將該導(dǎo)出對(duì)象設(shè)置為默認(rèn)導(dǎo)出對(duì)象。
4、導(dǎo)入和導(dǎo)出都可以使用as重新命名,as前為原來(lái)的名字,后為定義后的名字。
舉例:
import * as someIdentifier from "someModule"; *************************************** export { es6 as default } from './someModule'; // 等同于 import { es6 } from './someModule'; export default es6;
到此這篇關(guān)于Vuex的各個(gè)模塊封裝的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vuex 模塊封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 封裝自定義組件之tabal列表編輯單元格組件實(shí)例代碼
這篇文章主要介紹了vue 封裝自定義組件tabal列表編輯單元格組件實(shí)例代碼,需要的朋友可以參考下2017-09-09vue-cli項(xiàng)目?jī)?yōu)化方法- 縮短首屏加載時(shí)間
這篇文章主要介紹了vue-cli項(xiàng)目?jī)?yōu)化 縮短首屏加載時(shí)間,需要的朋友可以參考下2018-04-04vue動(dòng)態(tài)生成dom并且自動(dòng)綁定事件
本篇文章主要介紹了vue動(dòng)態(tài)生成dom并且自動(dòng)綁定事件,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04vue中用qrcode庫(kù)將超鏈接生成二維碼圖片的示例代碼
生成二維碼是一種常見(jiàn)的需求,無(wú)論是用于商業(yè)宣傳還是個(gè)人分享,二維碼都可以提供快速方便的方式來(lái)傳遞信息,在Vue框架中,我們可以使用qrcode庫(kù)來(lái)輕松地生成二維碼,本篇博文將介紹如何安裝qrcode庫(kù),并通過(guò)一個(gè)實(shí)際例子來(lái)展示如何生成二維碼,需要的朋友可以參考下2023-12-12ant desing vue table 實(shí)現(xiàn)可伸縮列的完整例子
最近在使用ant-design-vue做表格時(shí),遇到要做一個(gè)可伸縮列表格的需求,在網(wǎng)上一直沒(méi)有找到好的方法,于是小編動(dòng)手自己寫(xiě)個(gè)可以此功能,下面小編把a(bǔ)nt desing vue table 可伸縮列的實(shí)現(xiàn)代碼分享到腳本之家平臺(tái)供大家參考2021-05-05Vue中動(dòng)態(tài)引入圖片要是require的原因解析
require是一個(gè)node方法,用于引入模塊,JSON或本地文件,這篇文章主要介紹了vue中動(dòng)態(tài)引入圖片為什么要是require,需要的朋友可以參考下2022-10-10淺談Vue內(nèi)置component組件的應(yīng)用場(chǎng)景
這篇文章主要介紹了淺談Vue內(nèi)置component組件的應(yīng)用場(chǎng)景,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03