vue项目回顾3【数据管理vuex】

结构:

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import store_recommend from './recommend'

Vue.use(Vuex);

const store = new Vuex.Store({
  state:{
    width_s:document.documentElement.clientWidth,
    count:1,
    slider:{"isopen":false},
    whichsonglist:false,
    headNav:'head-nav1',
    loading:false,
    ellipsisState:{"open":false,"data":null},
    },
  getters:{
    headNav:state=>state.headNav,
    count:state=>state.count,
    loading:state=>state.loading,
    ellipsisState:state=>state.ellipsisState
  },
  mutations:{
    lodingShow:(state)=>{
      state.loading = true;
    },
    ellipsisFn:(state,item_)=>{
      state.ellipsisState.open = !state.ellipsisState.open;
      state.ellipsisState.data = item_;
    },
    tool_audio:(state,el)=>{
      state.play.play = el;
    },
    currentTime_Fn:(state,time)=>{
      state.play.currentTime_ = time;
    },
    setInterval:(state)=>{
      state.play.currentTime_+=1;
    }
  },
  actions:{
    actionsFn:({commit,state})=>{
      commit('aa');
      console.log('ppppp');
    }
  },
  modules:{
    sr:store_recommend
  }
});

export default store;

五部分:
state :注册数据管理参数
getters :用于获取参数
mutations :改变参数值的方法容器
actions :用来操作多个mutations 方法
modules :分模块管理数据

应用方法如下:
1.数据获取
store/index.js:

  state:{
    count:1
    }
  getters:{
    count:state=>state.count
  },

页面:

 <li> <i class="fa fa-address-book"></i>最近播放<span>{{count}}</span></li>

------------------------------------------------------
  import { mapGetters,mapState,mapActions} from 'vuex'
 export default {
      加入:
      computed:{
        ...mapGetters(['count'])
      },

或者:
只注册state然后直接:

thissong:this.$store.state.thissong.data

2.数据设置(mutations 相关)
不用引用,直接

 this.$store.commit('tool_audio',false);

3.actions 相关

 <li @click="testfn()"> 最近播放<span>23</span></li>

  methods:{
           ...mapActions({testfn:'actionsFn'})
}

相当于:

  methods:{
          testfn(){
              this.$store.dispatch('actionsFn')
          }
}

4.模块
引入模块是为了降低代码中的耦合性,也方便管理
在上文中我们引入了一个store_recommend的模块
state/index.js:

  modules:{
    sr:store_recommend
  }

state/store_recommend.js:

const store_recommend = {
  namespaced: true,
  state:{
    count:1
  },
  getters:{

  },
  mutations:{
    countFn:(state,count)=>{
      state.count+=count;
      }
  },
  actions:{

  }
}
export default store_recommend

应用页面:

 methods:{
        addcount(count_){
            this.$store.commit('sr/countFn',count_)
        }

好啦,该系列完毕嘿嘿

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、初识VueX 1.1 关于VueX VueX是适用于在Vue项目开发时使用的状态管理工具。试想一下,如果在一个...
    怪兽难吃素阅读 325,507评论 24 314
  • 1.什么是状态: 可以理解为在data中的属性需要分享给其他vue组件使用的部分,就叫做状态。简单的说就是data...
    JLong阅读 1,138评论 0 0
  • 一、什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有...
    紫月凌枫阅读 10,184评论 0 16
  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 2,970评论 0 7
  • Vue现代化使用方法(四)--Vuex 在组件内可以通过data属性共享数据,父子组件也可以通过props进行数据...
    去年的牛肉阅读 192评论 0 2