当前位置:   article > 正文

vuex使用完整版_怎么运行github的vuex的案例

怎么运行github的vuex的案例

场景2需要在vuex中大量管理数据

定义文件和变量

文件设置,在src下添加如下的文件目录

--store

---actions.js

---getters.js

---index.js

---mutation-types.js

---mutations.js

---state.js

第一步设计state.js,这里面确定需要被vuex管理的变量

state.js

  1. import {playMode} from '@/common/js/config.js' //获取项目的配置---可选
  2. const state={
  3. singer:{},
  4. playing:false,
  5. fullScreen:false,
  6. playList:[],
  7. sequenceList:[],
  8. mode:playMode.sequence,
  9. currentIndex:-1
  10. };
  11. export default state

第二步设置getters.js映射,也就是确定vuex中能够被取到的值

getters.js

  1. export const singer= state => state.singer; //简单点说就是把state.singer映射到常量singer上
  2. export const playing= state => state.playing;
  3. export const fullScreen= state => state.fullScreen;
  4. export const playList= state => state.playList;
  5. export const sequenceList= state => state.sequenceList;
  6. export const mode= state => state.mode;
  7. export const currentIndex= state => state.currentIndex;
  8. export const currentSong = (state)=>{ //getters同时承担了计算属性的功能,将state中的变量二次组装计算抛出新的变量让组件使用
  9. return state.playList[state.currentIndex] || {}
  10. };

第三步定义如何修改vuex中的值,mutations和actions都可以修改,那么我们需要先定义mutation-type

mutation-type.js

  1. export const SET_SINGER='SET_SINGER'; //定义修改动作名称,也就是mutations中修改数据的方法名
  2. export const SET_PLAYING_STATE='SET_PLAYING_STATE';
  3. export const SET_FULL_SCREEN='SET_FULL_SCREEN';
  4. export const SET_PLAYLIST='SET_PLAYLIST';
  5. export const SET_SEQUENCE_LIST='SET_SEQUENCE_LIST';
  6. export const SET_PLAY_MODE='SET_PLAY_MODE';
  7. export const SET_CURRENT_INDEX='SET_CURRENT_INDEX';

第四步定义mutations,定义vuex中数据的修改方法

mutations.js

  1. import * as types from './mutation-types'
  2. const mututations={
  3. [types.SET_SINGER](state,singer) {
  4. state.singer=singer
  5. },
  6. [types.SET_PLAYING_STATE](state,flag){
  7. state.playing=flag
  8. },
  9. [types.SET_FULL_SCREEN](state,flag){
  10. state.fullScreen=flag
  11. },
  12. [types.SET_PLAYLIST](state,list){
  13. state.playList=list
  14. },
  15. [types.SET_SEQUENCE_LIST](state,list){
  16. state.sequenceList=list
  17. },
  18. [types.SET_PLAY_MODE](state,mode){
  19. state.mode=mode
  20. },
  21. [types.SET_CURRENT_INDEX](state,index){
  22. state.currentIndex=index
  23. }
  24. };
  25. export default mututations;

第五步定义action----mutation通常是每个方法修改单个值,而action通常是每个方法同时修改多个值,action中还会做些异处理---这里经常是根据业务逻辑写方法,一般是之后边开发边写,开始的时候不需要定义

action.js

  1. import * as types from './mutation-types'
  2. export const selectPlay=function ({commit,state},{list,index}) { //selectPlay一个方法执行多个commit
  3. commit(types.SET_PLAYLIST,list);
  4. commit(types.SET_SEQUENCE_LIST,list);
  5. commit(types.SET_CURRENT_INDEX,index);
  6. commit(types.SET_FULL_SCREEN,true);
  7. commit(types.SET_PLAYING_STATE,true);
  8. };

第六部注册,所有的文件定义好了,就可以注册了

index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import * as actions from './actions'
  4. import * as getters from './getters'
  5. import state from './state'
  6. import mutations from './mutations'
  7. import createLogger from 'vuex/dist/logger' //辅助调试插件引入,在vuex中的值发生改变的时候会自动在控制台打出变量改变前后的值
  8. Vue.use(Vuex);
  9. const debug= process.env.NODE_ENV != 'production'
  10. export default new Vuex.Store({
  11. actions,
  12. getters,
  13. state,
  14. mutations,
  15. strict:debug,
  16. plugins:debug?[createLogger()]:[] //让调试插件只在开发时开启
  17. })

在main,js中

  1. import store from './store' //本质上是引入index.js
  2. new Vue({
  3. el: '#app',
  4. router,
  5. store, //store实例注册到vue实例中
  6. render: h => h(App)
  7. })

使用vuex中的变量

1getter---获取vuex中的变量并使用

  1. import {mapGetters} from 'vuex';
  2. computed:{
  3. ...mapGetters([ //获取暴露出vuex中的变量,这里暴露出来的变量直接当计算属性使用
  4. 'fullScreen',
  5. 'playList',
  6. 'currentSong',
  7. 'playing'
  8. ]),
  9. },

2mutation--修改vuex中的某个值

  1. import {mapMutations} from 'vuex';
  2. methods:{
  3. setFullScreen(){
  4. this.setFullScreen(true); //直接调用方法提交修改vuex中的变量
  5. },
  6. stopPlay(){
  7. this.setPlayingState(false);
  8. },
  9. ...mapMutations( //不能直接修改vuex中的变量,通过映射方法传参数的方式提交改变vuex中的参数
  10. {
  11. setFullScreen:'SET_FULL_SCREEN',
  12. setPlayingState:'SET_PLAYING_STATE'
  13. }
  14. )
  15. },

 

3action---使用actions.js中的方法改变vuex中的变量

  1. import {mapActions} from 'vuex' //action中定义一次改变vuex中多个变量的方法
  2. methods:{
  3. selectItem(song,index){
  4. this.selectPlay({ //直接调用并传入需要的参数
  5. list:this.songs,
  6. index:index
  7. })
  8. },
  9. ...mapActions([ //暴露出actions中的方法,作为方法直接调用
  10. 'selectPlay'
  11. ])
  12. },

简易版查看:https://blog.csdn.net/github_39274378/article/details/81542554

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/257056
推荐阅读
相关标签
  

闽ICP备14008679号