时间:2021-05-26
一、各个模块的作用:
二、 创建文件:
三、编辑文件
这里只是拿出自己的项目来做一个例子,只是介绍封装的方法。
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调试工具Vue.use(Vuex)const debug = process.env.NODE_ENV !== 'prodycution' // 开发环境下开启严格模式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 statemutations.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 mutationsmutation-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.singerexport const playing = state => state.playingexport const fullScreen = state => state.fullScreenexport const playlist = state => state.playlistexport const sequenceList = state => state.sequenceListexport const mode = state => state.modeexport const currentIndex = state => state.currentIndexexport const currentSong = (state) => { return state.playlist[state.currentIndex] || {}}export const disc = state => state.discexport const topList = state => state.topListexport const searchHistory = state => state.searchHistoryactions.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 // 记录当前歌曲 let currentSong = playlist[currentIndex] // 查找当前列表中是否有待插入的歌曲并返回其索引 let fpIndex = findIndex(playlist, song) // 因为是插入歌曲,所以索引要+1 currentIndex++ // 插入这首歌到当前索引位置 playlist.splice(currentIndex, 0, song) // 如果已经包含这首歌 if (fpIndex > -1) { // 如果当前插入的序号大于列表中的序号 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())}小贴士:
默认接口: export default
具名接口: export
1、export导出多个对象,export default只能导出一个对象。
2、export导出对象需要用{ },export default不需要{ }。
3、在其他文件引用export default导出的对象时不一定使用导出时的名字。因为这种方式实际上是将该导出对象设置为默认导出对象。
4、导入和导出都可以使用as重新命名,as前为原来的名字,后为定义后的名字。
举例:
到此这篇关于Vuex的各个模块封装的实现的文章就介绍到这了,更多相关Vuex 模块封装内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.实现思路(1)设置锁屏密码(2)密码存localStorage(本项目已经封装h5的sessionStorage和localStorage)(3)vuex设
为什么会出现VueX的模块呢?当你的项目中代码变多的时候,很难区分维护。那么这时候Vuex的模块功能就这么体现出来了。那么我们就开始吧!一、模块是啥?/*esl
一、前言在项目如何使用vuex呢?以前我都是非模块末去写的,可能大家和我一样也是这么去写,但是回过头去看看vue的文档,发现模块化去使用vuex更好,vue是单
问题:vuex分模块后,一个模块如何拿到其他模块的state值,调其他模块的方法?思路:1.通过命名空间取值--this.$store.state.car.li
1.Nuxt里怎么使用vuex?Nuxt.js内置引用了vuex模块,所以不需要额外安装。Nuxt.js会尝试找到应用根目录下的store目录,如果该目