1. vuex的定义
  2.  
  3. Vuex 是一个专门为 Vue.js 应用程序开发的状态管理模式,使用插件的形式引进项目中
  4.  
  5. )集中存储和管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化
  6.  
  7. )每一个 Vuex 应用的核心就是 store(仓库),new Vue.store({...}),“store”基本上就是一个容器,它包含应用中大部分的状态 (state)
  8.  
  9.  
  10.  
  11. vuex解决的问题
  12.  
  13. )多个视图依赖于同一状态
  14.  
  15. )来自不同视图的行为需要变更同一状态
  16.  
  17. Vuex则是把组件的共享状态抽取出来,以一个全局单例模式管理
  18.  
  19. 同时,通过定义和隔离状态管理中的各种概念并强制遵守一定的规则,代码变得更结构化、易维护
  20.  
  21. 以上就是vuex的思想
  22.  
  23.  
  24.  
  25. 、使用vuex的场景
  26.  
  27. 开发大型单页应用
  28.  
  29.  
  30.  
  31. vuex和全局对象的区别
  32.  
  33. Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。
  34.  
  35. )你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化,从而让我们能够实现一些工具帮助我们更好地了解我们的应用
  36.  
  37. vuex中的5个核心概念
  38.  
  39. index.js引进这5个概念的定义
  40.  
  41. import Vue from 'vue'
  42. import Vuex from 'vuex'
  43. import * as actions from './actions'
  44. import * as getters from './getters'
  45. import state from './state'
  46. import mutations from './mutations'
  47. import createLogger from 'vuex/dist/logger'
  48.  
  49. Vue.use(Vuex)
  50.  
  51. const debug = process.env.NODE_ENV !== 'production'
  52.  
  53. export default new Vuex.Store({
  54. actions,
  55. getters,
  56. state,
  57. mutations,
  58. strict: debug,
  59. plugins: debug ? [createLogger()] : []
  60. })
  61. 然后,在main.js中引进index.js,并在全局Vue实例下注册该store对象,这样所有的组件都可以共享store对象里面的state
  62.  
  63. import store from './store'
  64. new Vue({
  65. el: '#app',
  66. router,
  67. store,
  68. render: h => h(App)
  69. })
  70.  
  71.  
  72.  
  73.  
  74. state对象:(在computed中引入)Vuex 使用单一状态树——用一个对象就包含了全部的应用层级状态;每个应用将仅仅包含一个 store 实例
  75.  
  76. computed: {
  77. localComputed () { /* ... */ },
  78. // 使用对象展开运算符将此对象混入到外部对象中
  79. ...mapState({
  80. // ...
  81. })
  82. }
  83. state.js文件的内容 ----定义的states是在getters.js    mutations.js    actions.js文件调用
  84.  
  85. import {playMode} from 'common/js/config'
  86. import {loadSearch, loadPlay, loadFavorite} from 'common/js/cache'
  87.  
  88. const state = {
  89. singer: {},
  90. playing: false,
  91. fullScreen: false,
  92. playlist: [],
  93. sequenceList: [],
  94. mode: playMode.sequence,
  95. currentIndex: -,
  96. disc: {},
  97. topList: {},
  98. searchHistory: loadSearch(),
  99. playHistory: loadPlay(),
  100. favoriteList: loadFavorite()
  101. }
  102.  
  103. export default state
  104.  
  105.  
  106. getters对象:(可以认为是store的计算属性),在组件中的computed中引入
  107.  
  108.  
  109.  
  110. getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算
  111.  
  112. Getter 接受 state 作为其第一个参数,也可以接受其他 getter 作为第二个参数
  113.  
  114. mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性
  115.  
  116. computed: {
  117. // 使用对象展开运算符将 getter 混入 computed 对象中
  118. ...mapGetters([
  119. 'doneTodosCount',
  120. 'anotherGetter',
  121. // ...
  122. ])
  123. }
  124. getter 属性另取一个名字,使用对象形式
  125.  
  126. mapGetters({
  127. // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
  128. doneCount: 'doneTodosCount'
  129. })
  130. getters.js文件内容 ---输出states
  131.  
  132. //获取数据
  133. export const singer = state => state.singer
  134.  
  135. export const playing = state => state.playing
  136.  
  137. export const fullScreen = state => state.fullScreen
  138.  
  139. export const playlist = state => state.playlist
  140.  
  141. export const sequenceList = state => state.sequenceList
  142.  
  143. export const mode = state => state.mode
  144.  
  145. export const currentIndex = state => state.currentIndex
  146.  
  147. export const currentSong = (state) => {
  148. return state.playlist[state.currentIndex] || {}
  149. }
  150.  
  151. export const disc = state => state.disc
  152.  
  153. export const topList = state => state.topList
  154.  
  155. export const searchHistory = state => state.searchHistory
  156.  
  157. export const playHistory = state => state.playHistory
  158.  
  159. export const favoriteList = state => state.favoriteList
  160. 在组件里面获取states
  161.  
  162. // 在组件里面引进states
  163. computed: {
  164. ...mapGetters([
  165. 'currentIndex',
  166. 'fullScreen',
  167. 'playing'
  168. ])
  169. }
  170.  
  171. //在组件里面调用state
  172. let index = this.currentIndex -
  173.  
  174.  
  175.  
  176.  
  177. mutations对象:更改 Vuex store 的状态的唯一方法是commit mutation;
  178.  
  179. mutation类似于事件,每个 mutation 都有一个字符串的 事件类型 (type)  一个 回调函数 (handler);
  180.  
  181. 回调函数对状态进行更改, state 作为第一个参数
  182.  
  183. mutation-types.js文件内容----定义一些事件名,用常量表示
  184.  
  185. export const SET_SINGER = 'SET_SINGER'
  186.  
  187. export const SET_PLAYING_STATE = 'SET_PLAYING_STATE'
  188.  
  189. export const SET_FULL_SCREEN = 'SET_FULL_SCREEN'
  190.  
  191. export const SET_PLAYLIST = 'SET_PLAYLIST'
  192.  
  193. export const SET_SEQUENCE_LIST = 'SET_SEQUENCE_LIST'
  194.  
  195. export const SET_PLAY_MODE = 'SET_PLAY_MODE'
  196.  
  197. export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX'
  198.  
  199. export const SET_DISC = 'SET_DISC'
  200.  
  201. export const SET_TOP_LIST = 'SET_TOP_LIST'
  202.  
  203. export const SET_SEARCH_HISTORY = 'SET_SEARCH_HISTORY'
  204.  
  205. export const SET_PLAY_HISTORY = 'SET_PLAY_HISTORY'
  206.  
  207. export const SET_FAVORITE_LIST = 'SET_FAVORITE_LIST'
  208. mutations.js文件内容-----提交一个state的修改
  209.  
  210. import * as types from './mutation-types'
  211. //mutations里面存放的是方法名
  212. const mutations = {
  213. [types.SET_SINGER](state, singer) {
  214. //能够获取到当前状态树的state,提交mutation的时候传的参数
  215. state.singer = singer
  216. },
  217. [types.SET_PLAYING_STATE](state, flag) {
  218. state.playing = flag
  219. },
  220. [types.SET_FULL_SCREEN](state, flag) {
  221. state.fullScreen = flag
  222. },
  223. [types.SET_PLAYLIST](state, list) {
  224. state.playlist = list
  225. },
  226. [types.SET_SEQUENCE_LIST](state, list) {
  227. state.sequenceList = list
  228. },
  229. [types.SET_PLAY_MODE](state, mode) {
  230. state.mode = mode
  231. },
  232. [types.SET_CURRENT_INDEX](state, index) {
  233. state.currentIndex = index
  234. }
  235. }
  236. export default mutations
  237. 在组件里面commit  mutation
  238.  
  239. methods:{
  240. // 引进一个mutation
  241. ...mapMutations({
  242. setFullScreen: 'SET_FULL_SCREEN'
  243. })
  244.  
  245. // 修改了一个state,然后commit mutation
  246. back() {
  247. this.setFullScreen(false)
  248. }
  249. }
  250. 不能直接调用mutation,需要使用store.commit(mutation)来调用
  251.  
  252. 提交载荷:可以向 store.commit 传入额外的参数,即 mutation  载荷(payload),作为mutation的第二个参数
  253.  
  254.  
  255.  
  256.  
  257.  
  258. 对象风格的提交方式:直接使用包含 type 属性的对象
  259.  
  260.  
  261.  
  262. store.commit({
  263. type: 'increment',
  264. amount:
  265. })
  266. mutations需遵守Vue的响应规则:
  267.  
  268. )最好提前在 store实例化 中初始化好所有所需属性
  269.  
  270. )在对象上添加新的属性的方法:
  271.  
  272. Vue.set(obj, 'newProp', )
  273. //以新对象替换老对象,使用对象展开运算符
  274. state.obj = { ...state.obj, newProp: }
  275. 使用常量替代mutation类型,通常在mutation-types中定义
  276. mutation是同步函数:实质上任何在回调函数中进行的状态的改变都是不可追踪
  277.  
  278.  
  279.  
  280. 在组件中提交mutation
  281.  
  282.  
  283.  
  284. this.$store.commit('xxx') 提交 mutation
  285. 使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store
  286. methods: {
  287. ...mapMutations([
  288. 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
  289.  
  290. // `mapMutations` 也支持载荷:
  291. 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
  292. ]),
  293. ...mapMutations({
  294. add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
  295. })
  296. }
  297. actions对象:actions类似mutations,不同之处
  298.  
  299.  
  300.  
  301. action 提交的是 mutationmutation直接变更状态。
  302. action 可以包含任意异步操作
  303. 当某个动作触发多个mutation的时候使用action
  304.  
  305. 每一个action 函数接受一个与 store 实例具有相同方法和属性的对象,比如叫做context对象,因此可以调用 context.commit 提交一个 mutation,通过 context.state context.getters 来获取 state getters
  306.  
  307. 使用参数结构来简化代码:
  308.  
  309. actions: {
  310. increment (context,payload) {
  311. commit('increment')
  312. }
  313. }
  314. 实际上可以写成类似下面的形式,使用参数结构的方法来简化代码
  315. actions: {
  316. increment ({ commitstate },{m,n}) {
  317. commit('increment')
  318. }
  319. }
  320. 分发actionstore.dispatch,在action内部执行异步操作
  321.  
  322. actions: {
  323. incrementAsync ({ commit }) {
  324. setTimeout(() => {
  325. commit('increment')
  326. }, )
  327. }
  328. }
  329. Actions 支持载荷方式和对象方式进行分发
  330.  
  331. // 以载荷形式分发
  332. store.dispatch('incrementAsync', {
  333. amount:
  334. })
  335.  
  336. // 以对象形式分发
  337. store.dispatch({
  338. type: 'incrementAsync',
  339. amount:
  340. })
  341. 在组件中分发action
  342.  
  343. this.$store.dispatch('xxx') 分发 action
  344. 使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store
  345. actions.js文件内容 -----可以同时修改多个states,然后commit 多个states
  346.  
  347. import * as types from './mutation-types'
  348. import {playMode} from 'common/js/config'
  349. import {shuffle} from 'common/js/util'
  350. import {saveSearch, clearSearch, deleteSearch, savePlay, saveFavorite, deleteFavorite} from 'common/js/cache'
  351.  
  352. function findIndex(list, song) {
  353. return list.findIndex((item) => {
  354. return item.id === song.id
  355. })
  356. }
  357.  
  358. export const selectPlay = function ({commit, state}, {list, index}) {
  359. commit(types.SET_SEQUENCE_LIST, list)
  360. if (state.mode === playMode.random) {
  361. let randomList = shuffle(list)
  362. commit(types.SET_PLAYLIST, randomList)
  363. index = findIndex(randomList, list[index])
  364. } else {
  365. commit(types.SET_PLAYLIST, list)
  366. }
  367. commit(types.SET_CURRENT_INDEX, index)
  368. commit(types.SET_FULL_SCREEN, true)
  369. commit(types.SET_PLAYING_STATE, true)
  370. }
  371.  
  372. export const randomPlay = function ({commit}, {list}) {
  373. commit(types.SET_PLAY_MODE, playMode.random)
  374. commit(types.SET_SEQUENCE_LIST, list)
  375. let randomList = shuffle(list)
  376. commit(types.SET_PLAYLIST, randomList)
  377. commit(types.SET_CURRENT_INDEX, )
  378. commit(types.SET_FULL_SCREEN, true)
  379. commit(types.SET_PLAYING_STATE, true)
  380. }
  381.  
  382. export const insertSong = function ({commit, state}, song) {
  383. let playlist = state.playlist.slice()
  384. let sequenceList = state.sequenceList.slice()
  385. let currentIndex = state.currentIndex
  386. // 记录当前歌曲
  387. let currentSong = playlist[currentIndex]
  388. // 查找当前列表中是否有待插入的歌曲并返回其索引
  389. let fpIndex = findIndex(playlist, song)
  390. // 因为是插入歌曲,所以索引+1
  391. currentIndex++
  392. // 插入这首歌到当前索引位置
  393. playlist.splice(currentIndex, , song)
  394. // 如果已经包含了这首歌
  395. if (fpIndex > -) {
  396. // 如果当前插入的序号大于列表中的序号
  397. if (currentIndex > fpIndex) {
  398. playlist.splice(fpIndex, )
  399. currentIndex--
  400. } else {
  401. playlist.splice(fpIndex + , )
  402. }
  403. }
  404.  
  405. let currentSIndex = findIndex(sequenceList, currentSong) +
  406.  
  407. let fsIndex = findIndex(sequenceList, song)
  408.  
  409. sequenceList.splice(currentSIndex, , song)
  410.  
  411. if (fsIndex > -) {
  412. if (currentSIndex > fsIndex) {
  413. sequenceList.splice(fsIndex, )
  414. } else {
  415. sequenceList.splice(fsIndex + , )
  416. }
  417. }
  418.  
  419. commit(types.SET_PLAYLIST, playlist)
  420. commit(types.SET_SEQUENCE_LIST, sequenceList)
  421. commit(types.SET_CURRENT_INDEX, currentIndex)
  422. commit(types.SET_FULL_SCREEN, true)
  423. commit(types.SET_PLAYING_STATE, true)
  424. }
  425.  
  426. export const saveSearchHistory = function ({commit}, query) {
  427. commit(types.SET_SEARCH_HISTORY, saveSearch(query))
  428. }
  429.  
  430. export const deleteSearchHistory = function ({commit}, query) {
  431. commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
  432. }
  433.  
  434. export const clearSearchHistory = function ({commit}) {
  435. commit(types.SET_SEARCH_HISTORY, clearSearch())
  436. }
  437.  
  438. export const deleteSong = function ({commit, state}, song) {
  439. let playlist = state.playlist.slice()
  440. let sequenceList = state.sequenceList.slice()
  441. let currentIndex = state.currentIndex
  442. let pIndex = findIndex(playlist, song)
  443. playlist.splice(pIndex, )
  444. let sIndex = findIndex(sequenceList, song)
  445. sequenceList.splice(sIndex, )
  446. if (currentIndex > pIndex || currentIndex === playlist.length) {
  447. currentIndex--
  448. }
  449.  
  450. commit(types.SET_PLAYLIST, playlist)
  451. commit(types.SET_SEQUENCE_LIST, sequenceList)
  452. commit(types.SET_CURRENT_INDEX, currentIndex)
  453.  
  454. if (!playlist.length) {
  455. commit(types.SET_PLAYING_STATE, false)
  456. } else {
  457. commit(types.SET_PLAYING_STATE, true)
  458. }
  459. }
  460.  
  461. export const deleteSongList = function ({commit}) {
  462. commit(types.SET_CURRENT_INDEX, -)
  463. commit(types.SET_PLAYLIST, [])
  464. commit(types.SET_SEQUENCE_LIST, [])
  465. commit(types.SET_PLAYING_STATE, false)
  466. }
  467.  
  468. export const savePlayHistory = function ({commit}, song) {
  469. commit(types.SET_PLAY_HISTORY, savePlay(song))
  470. }
  471.  
  472. export const saveFavoriteList = function ({commit}, song) {
  473. commit(types.SET_FAVORITE_LIST, saveFavorite(song))
  474. }
  475.  
  476. export const deleteFavoriteList = function ({commit}, song) {
  477. commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
  478. }
  479. 在组件里面调用 
  480.  
  481. methods:{
  482. //引进actions
  483. ...mapActions([
  484. 'savePlayHistory'
  485. ])
  486.  
  487. //修改多个states
  488. ready() {
  489. this.savePlayHistory(this.currentSong)
  490. }
  491. }
  492.  
  493.  
  494. import { mapActions } from 'vuex'
  495.  
  496. export default {
  497. // ...
  498. methods: {
  499. ...mapActions([
  500. 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
  501.  
  502. // `mapActions` 也支持载荷:
  503. 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
  504. ]),
  505. ...mapActions({
  506. add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  507. })
  508. }
  509. }
  510. Module:使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 statemutationactiongetter、甚至是嵌套子模块——从上至下进行同样方式的分割
  511.  
  512.  
  513.  
  514. const moduleA = {
  515. state: { ... },
  516. mutations: { ... },
  517. actions: { ... },
  518. getters: { ... }
  519. }
  520.  
  521. const moduleB = {
  522. state: { ... },
  523. mutations: { ... },
  524. actions: { ... }
  525. }
  526.  
  527. const store = new Vuex.Store({
  528. modules: {
  529. a: moduleA,
  530. b: moduleB
  531. }
  532. })
  533.  
  534. store.state.a // -> moduleA 的状态
  535. store.state.b // -> moduleB 的状态
  536.  
  537.  
  538. 总结:在使用vuex的时候,如果使用模块化的思想来编程的话,那么通常情况下就需要定义6js文件,分别是
  539.  
  540.  
  541.  
  542. index.js state.js getters.js mutation-types.js mutation.js actions.js
  543. index.js:在这里面去引入其他5JS文件,然后export 一个Vuex.store({})的实例对象,然后再main.js文件里面importjs文件,在Vue实例里面添加store选项,即在根节点下注册该store对象
  544.  
  545. //index.js文件里面的内容
  546. //store文件夹下,编写vuex的相关代码
  547. //index.js是vuex的入口文件
  548. //vuex中的state只能通过commit修改,不能直接修改
  549. import Vue from 'vue'
  550. import Vuex from 'vuex'
  551. //起别名是为了像调用对象的方法一样,调用模块里面定义的方法
  552. import * as actions from './actions'
  553. import * as getters from './getters'
  554. import state from './state'
  555. import mutations from './mutations'
  556. //使用vuex提供的一些插件
  557. //通过mutation方式修改state的时候,他会在console中输出日志
  558. import createLogger from 'vuex/dist/logger'
  559. //注册插件Vuex
  560. Vue.use(Vuex)
  561. //npm run dev则process.env.NODE_EN=develop
  562. //npm run builde则process.env.NODE_EN=production
  563. //有性能损耗,所以在开发环境中使用
  564. const debug = process.env.NODE_ENV !== 'production'
  565. //输出一个实例化的vuex对象
  566. export default new Vuex.Store({
  567. actions,
  568. getters,
  569. state,
  570. mutations,
  571. strict: debug,
  572. plugins: debug ? [createLogger()] : []
  573. })
  574. state.js:在这里面定义一些组件之间的公用状态变量对象,然后export state对象
  575.  
  576.  
  577.  
  578. //所有的状态都放在这个文件夹下管理
  579. //也就是存放数据的对象
  580. //这里要管理的是singer
  581. //playMode对象里面定义了一些常量
  582. import {playMode} from '../common/js/config'
  583.  
  584. const state = {
  585. singer: {},
  586. //播放的状态
  587. playing: false,
  588. //歌单展开控制
  589. fullScreen: false,
  590. //播放的是一个列表
  591. playlist: [],
  592. //播放的歌单是一个有顺序的列表
  593. sequenceList: [],
  594. //设置播放模式,顺序播放,随机播放
  595. mode: playMode.sequence,
  596. //当前播放的一个索引
  597. currentIndex: -
  598. }
  599. export default state
  600. getters.js:在这里面定义一些常量变量,它的值是函数的返回值,用于获取state.js中的对象的值;或者是当做store对象的计算属性,对state.js的属性对象进行计算,得到一些计算结果的变量,export多个常量
  601.  
  602.  
  603.  
  604. //获取states里面的属性,会做一些映射
  605. //或者作为计算属性,根据states中的属性,计算得到另外一些属性
  606. //代理计算属性,在组件中在computed中可以通过...mapGetters([])引入
  607. //或mapGetters({}),给函数取别名
  608. //getter的第一个参数为state,第二个可选是getter
  609. export const singer = state => state.singer
  610. export const playing = state => state.playing
  611. export const fullScreen = state => state.fullScreen
  612. export const playlist = state => state.playlist
  613. export const sequenceList = state => state.sequenceList
  614. export const mode = state => state.mode
  615. export const currentIndex = state => state.currentIndex
  616. export const currentSong = state => {
  617. return state.playlist[state.currentIndex] || {}
  618. }
  619. mutation-types.js:在这里面定义一些操作mutation的字符串常量变量名,这里是定义一些动作名,所以名字一般是set  或者是update,然后再结合state.js中定义的变量对象,export多个常量
  620.  
  621.  
  622.  
  623. //存储mutation的一些相关的名字,也就是一些字符串常量
  624. //即使用常量替代mutation类型
  625. //mutations里面定义一些方法,这里也就是定义mutations里面方法的名字
  626. // 一些动作,所以用set,update
  627. export const SET_SINGER = 'SET_SINGER'
  628. export const SET_PLAYING_STATE = 'SET_PLAYING_STATE'
  629. export const SET_FULL_SCREEN = 'SET_FULL_SCREEN'
  630. export const SET_PLAYLIST = 'SET_PLAYLIST'
  631. export const SET_SEQUENCE_LIST = 'SET_SEQUENCE_LIST'
  632. export const SET_PLAY_MODE = 'SET_PLAY_MODE'
  633. export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX'
  634. mutations.js:在这里面定义一些修改state.js中变量对象的值的方法,这些方法都放在一个常量mutations对象里面,然后export mutations
  635.  
  636.  
  637.  
  638. //输出一个mutations对象,
  639. //每一个mutation的第一个参数state,第二个参数是payload
  640. //用于改变state中的属性的状态
  641. //这些操作都是同步操作
  642. //在组件的methods中通过...mapMutations([])或者...mapMutations({})--用于改别名
  643. import * as types from './mutation-types'
  644. //mutations里面存放的是方法名
  645. const mutations = {
  646. [types.SET_SINGER](state, singer) {
  647. //能够获取到当前状态树的state,提交mutation的时候传的参数
  648. state.singer = singer
  649. },
  650. [types.SET_PLAYING_STATE](state, flag) {
  651. state.playing = flag
  652. },
  653. [types.SET_FULL_SCREEN](state, flag) {
  654. state.fullScreen = flag
  655. },
  656. [types.SET_PLAYLIST](state, list) {
  657. state.playlist = list
  658. },
  659. [types.SET_SEQUENCE_LIST](state, list) {
  660. state.sequenceList = list
  661. },
  662. [types.SET_PLAY_MODE](state, mode) {
  663. state.mode = mode
  664. },
  665. [types.SET_CURRENT_INDEX](state, index) {
  666. state.currentIndex = index
  667. }
  668. }
  669. export default mutations
  670. actions.js:在这里面定义一些常量方法,每个常量方法用于提交多个mutation,然后输出多个常量方法名
  671.  
  672.  
  673.  
  674. //当一个动作会触发多个mutation则在actions里面定义一个方法,从而触发这些mutation
  675. import * as types from './mutation-types'
  676. import {playMode} from '../common/js/config'
  677. import {shuffle} from '../common/js/util'
  678. function findIndex(list, song) {
  679. return list.findIndex((item) => {
  680. return item.id === song.id
  681. })
  682. }
  683. export const selectPlay = function ({commit, state}, {list, index}) {
  684. commit(types.SET_SEQUENCE_LIST, list)
  685. if (state.mode === playMode.random) {
  686. let randomList = shuffle(list)
  687. commit(types.SET_PLAYLIST, randomList)
  688. //将在顺序列表中播放的歌,在随机播放列表中的index
  689. //确保点击“随机播放按钮”的时候还是当前这首歌
  690. index = findIndex(randomList, list[index])
  691. } else {
  692. commit(types.SET_PLAYLIST, list)
  693. }
  694. commit(types.SET_CURRENT_INDEX, index)
  695. commit(types.SET_FULL_SCREEN, true)
  696. commit(types.SET_PLAYING_STATE, true)
  697. }
  698. //定义一个随机播放的action
  699. export const randomPlay = function ({commit}, {list}) {
  700. commit(types.SET_PLAY_MODE, playMode.random)
  701. commit(types.SET_SEQUENCE_LIST, list)
  702. let randomList = shuffle(list)
  703. commit(types.SET_PLAYLIST, randomList)
  704. commit(types.SET_CURRENT_INDEX, )
  705. commit(types.SET_FULL_SCREEN, true)
  706. commit(types.SET_PLAYING_STATE, true)
  707.  
  708. ---------------------
  709. 作者:tangxiujiang
  710. 来源:CSDN
  711. 原文:https://blog.csdn.net/tangxiujiang/article/details/80645416
  712. 版权声明:本文为博主原创文章,转载请附上博文链接!

vuex的简单介绍的更多相关文章

  1. Vuex与axios介绍

    Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...

  2. [原创]关于mybatis中一级缓存和二级缓存的简单介绍

    关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...

  3. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  4. 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍

    一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...

  5. yii2的权限管理系统RBAC简单介绍

    这里有几个概念 权限: 指用户是否可以执行哪些操作,如:编辑.发布.查看回帖 角色 比如:VIP用户组, 高级会员组,中级会员组,初级会员组 VIP用户组:发帖.回帖.删帖.浏览权限 高级会员组:发帖 ...

  6. angular1.x的简单介绍(二)

    首先还是要强调一下DI,DI(Denpendency Injection)伸手获得,主要解决模块间的耦合关系.那么模块是又什么组成的呢?在我看来,模块的最小单位是类,多个类的组合就是模块.关于在根模块 ...

  7. Linux的简单介绍和常用命令的介绍

    Linux的简单介绍和常用命令的介绍 本说明以Ubuntu系统为例 Ubuntu系统的安装自行百度,或者参考http://www.cnblogs.com/CoderJYF/p/6091068.html ...

  8. iOS-iOS开发简单介绍

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...

  9. iOS开发多线程篇—多线程简单介绍

    iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...

随机推荐

  1. php fopen用法以及解释

    $log = fopen('./log.txt','a');//生成一个log.txt文件,a相当于文件的权限 fwrite($log,'成功'."\r\n");//写入文件 mo ...

  2. cucumber:extentreports集成报告

    extentreports 测试报告 只支持java..Net 先在pom.xml文件中加入包引用 <!-- report--> <dependency> <groupI ...

  3. bzoj1096题解

    [解题思路] 预处理spi=∑pj(j∈[1,i]),si=si-1+(xi-xi-1)*spi-1表示把工厂1~i-1的产品都运到工厂i的花费.于是把工厂j+1~i的产品都运到工厂i的花费为si-s ...

  4. HAProxy服务器 、Keepalived热备 、Keepalived+LVS

    配置HAProxy负载平衡集群 1.1 问题 准备三台Linux服务器,两台做Web服务器,一台安装HAProxy,实现如下功能: 客户端访问HAProxy,HAProxy分发请求到后端Real Se ...

  5. 50 ubuntu下pcl编译以及用 VSCode配置pcl / opencv开发环境

    0 引言 最近在VSCode下搞开发,于是将pcl库迁移到这个环境下,用来跑一些依赖pcl的开源的代码以及自己做一些快速开发等. 1 pcl编译 主要参考了这篇博客,链接如下. https://blo ...

  6. hive的复合数据类型

    hive中的复合数据类型 Array array中的数据为相同类型,例如,假如array A中元素['a','b','c'],则A[1]的值为'b' 数据结构如下: zhangsan beijing, ...

  7. NOIp2018集训test-9-19(am&pm)

    AM 这是一套在长沙考过而且我能记得全部正解的题,然后期望得分300实际得分155. T1 很套路,随便搞(我当年是怎么花大半场时间写T1并且写出现在两倍长的代码的??) //Achen #inclu ...

  8. Feign远程调用源码阅读

  9. NX二次开发-UFUN创建球UF_MODL_create_sphere1

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建球 UF_FEATURE_SIGN ...

  10. NX二次开发-如何判断孔特征和边倒圆是否在凸台特征后面?

    在建模的时候,部件导航器里的特征按建模顺序是有特征时间戳记的. 我们可以根据特征时间戳记的名字来判断哪个特征在前,哪个在后. #include <uf.h> #include <uf ...