欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vuex的各个模块封装的实现

程序员文章站 2022-07-06 18:28:18
一、各个模块的作用: state 用来数据共享数据存储 mutation 用来注册改变数据状态(同步) getters 用来对共享数据进行过滤并计数操作 action 解决异步改变共享数据(异...

一、各个模块的作用:

  • state 用来数据共享数据存储
  • mutation 用来注册改变数据状态(同步)
  • getters 用来对共享数据进行过滤并计数操作
  • action 解决异步改变共享数据(异步)

二、 创建文件:

  • actions.js
  • getters.js
  • index.js
  • mutations.js
  • mutation-types.js
  • state.js

三、编辑文件

这里只是拿出自己的项目来做一个例子,只是介绍封装的方法。

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 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
 // 记录当前歌曲
 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前为原来的名字,后为定义后的名字。

举例:

import * as someidentifier from "somemodule";
***************************************
export { es6 as default } from './somemodule';
// 等同于
import { es6 } from './somemodule';
export default es6;

到此这篇关于vuex的各个模块封装的实现的文章就介绍到这了,更多相关vuex 模块封装内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!