最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vuex的各個模塊封裝的實現(xiàn)

 更新時間:2020年06月05日 11:04:11   作者:Vam的金豆之路  
這篇文章主要介紹了Vuex的各個模塊封裝的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、各個模塊的作用:

  • state 用來數(shù)據(jù)共享數(shù)據(jù)存儲
  • mutation 用來注冊改變數(shù)據(jù)狀態(tài)(同步)
  • getters 用來對共享數(shù)據(jù)進行過濾并計數(shù)操作
  • action 解決異步改變共享數(shù)據(jù)(異步)

二、 創(chuàng)建文件:

  • 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調(diào)試工具

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'prodycution' // 開發(fā)環(huá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
 // 記錄當前歌曲
 let currentSong = playlist[currentIndex]

 // 查找當前列表中是否有待插入的歌曲并返回其索引
 let fpIndex = findIndex(playlist, song)
 // 因為是插入歌曲,所以索引要+1
 currentIndex++
 // 插入這首歌到當前索引位置
 playlist.splice(currentIndex, 0, song)
 // 如果已經(jīng)包含這首歌
 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;

到此這篇關(guān)于Vuex的各個模塊封裝的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vuex 模塊封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • vue3如何實現(xiàn)掛載并使用axios

    vue3如何實現(xiàn)掛載并使用axios

    這篇文章主要介紹了vue3如何實現(xiàn)掛載并使用axios,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 基于Vue.js實現(xiàn)簡潔的多屏切換效果

    基于Vue.js實現(xiàn)簡潔的多屏切換效果

    在實際開發(fā)中,多屏切換是常見的需求,尤其是在需要展示大量內(nèi)容或信息時,下面我將向大家展示我是如何實現(xiàn)三屏,并通過動態(tài)按鈕控制切換屏幕的,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • 關(guān)于net?6+vue?插件axios?后端接收參數(shù)問題

    關(guān)于net?6+vue?插件axios?后端接收參數(shù)問題

    接到這樣一個項目需求是這樣的,前端vue?必須對象傳遞后端也必須對象接收,接下來通過本文給大家介紹下net?6+vue?插件axios?后端接收參數(shù)的問題,需要的朋友可以參考下
    2022-01-01
  • vue實現(xiàn)按鈕切換圖片

    vue實現(xiàn)按鈕切換圖片

    這篇文章主要為大家詳細介紹了vue實現(xiàn)按鈕切換圖片,正向反向以及順序切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 關(guān)于vue2響應式缺陷的問題

    關(guān)于vue2響應式缺陷的問題

    這篇文章主要介紹了關(guān)于vue2響應式缺陷的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 詳解vue2.0+vue-video-player實現(xiàn)hls播放全過程

    詳解vue2.0+vue-video-player實現(xiàn)hls播放全過程

    這篇文章主要介紹了詳解vue2.0+vue-video-player實現(xiàn)hls播放全過程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue axios登錄請求攔截器

    vue axios登錄請求攔截器

    這篇文章主要介紹了vue axios登錄請求攔截器,判斷是否登錄超時,或?qū)φ埱蠼Y(jié)果做一個統(tǒng)一處理的教程詳解,需要的朋友可以參考下
    2018-04-04
  • vue3.0+echarts實現(xiàn)立體柱圖

    vue3.0+echarts實現(xiàn)立體柱圖

    這篇文章主要為大家詳細介紹了vue3.0+echarts實現(xiàn)立體柱圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue3中pinia的使用方法

    vue3中pinia的使用方法

    Pinia是Vue3的狀態(tài)管理工具,安裝后在入口文件引入,定義store并在組件中使用,本文就來介紹一下vue3中pinia的使用方法,感興趣的可以了解一下
    2024-10-10
  • vue子組件設計provide和inject理解使用

    vue子組件設計provide和inject理解使用

    這篇文章主要為大家介紹了vue子組件設計provide和inject理解及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08

最新評論

慈利县| 北宁市| 锦州市| 台州市| 黎川县| 新兴县| 乐都县| 城步| 通辽市| 莲花县| 如东县| 法库县| 宜丰县| 蓬莱市| 敦化市| 阿城市| 宜兰市| 都匀市| 岳池县| 进贤县| 手游| 龙胜| 汝城县| 秦皇岛市| 建宁县| 中超| 永兴县| 黄骅市| 白水县| 潜江市| 棋牌| 诸城市| 淳化县| 黄龙县| 西峡县| 呼伦贝尔市| 潜江市| 南华县| 黑龙江省| 城口县| 潞城市|