vuex方法

    网站项目,多页应用,webpage,对兼容性要求高
    webapp,应用程序,交互很多,“购物网站”
    后台管理,网站的后台管理,企业的资源管理系统
    网页游戏
    vue和react适合后台管理,webapp也适合,vue有weex,react有reactnative

    【Show】,show为方法名,可以将其设为常量,并用一个模块统一放置
##Vuex状态管理
    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态
    state,驱动应用的数据源;
    view,以声明方式将state映射到视图;
    actions,响应在view上的用户输入导致的状态变化。
##Vuex步骤
###1,首先下载依赖npm install vuex --save
    一个node package有两种依赖,一种是dependencies一种是devDependencies,其中前者依赖的项该是正常运行该包时所需要的依赖项,而后者则是开发的时候需要的依赖项,像一些进行单元测试之类的包。

    npm install module-name -save自动把模块和版本号添加到dependencies部分
    npm install module-name -save-dve自动把模块和版本号添加到devDependencies部分
    npm install module-name 那么只会安装dependencies部分
###2,新建一个store文件夹,用来存储相关文件
####1,index.js文件,
    首先引入相关模块,Vue和Vuex,Vue.use(Vuex)括号里面为需要用到Vuex,
    在里面写相关步骤
    import Vue from "vue";
    import Vuex from "vuex";
    import studentStore from "./student"
    Vue.use(Vuex);//用vuex
    const store = new Vuex.Store({//注意Store首字母大写
    state:{
        count:0
    },
    modules:{
        studentStore
    },
    mutations:{
        addCount(state){
            state.count++;
        }
    }
    });
    export {store as default}
#####mutations
    更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutations 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:

    你不能直接调用一个 mutation handler。这个选项更像是事件注册:“当触发一个类型为 addCount 的 mutation 时,调用此函数。”要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法:

    store.commit('addCount'),在需要存储的数据里面,传递相关函数。
#####modules
    里面为mutations里面的函数名,通过在引入student文件里面定义的mutation模块,来达到分散管理的作用。
###2,mutations.js文件
    const SHOW_DATA = "SHOW_DATA";
    const SHOW_STUDENT = "SHOW_STUDENT";
    export {SHOW_DATA,SHOW_STUDENT}
    用常量的方式来存储index.js里面的mutations需要用到的函数名字,方便查询管理和调用
###3,student.js文件
    import {SHOW_DATA,SHOW_STUDENT} from "./mutations";
    const studentStore = {
    state:{
        data:{rows:[]},
        student:{}
    },
    mutations:{
        [SHOW_DATA](state,data){
            state.data = data;
        },
        [SHOW_STUDENT](state,student){
            state.student = student;
        }
    }
    }
    export {studentStore as default}
    用来存放mutations模块

###4,main.js
    import Vue from 'vue'
    import router from './router'
    import Index from "@/components/Index"
    import store from "@/store"
    Vue.config.productionTip = false
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      router,
      store,//在最外层套上store下的index.js模块
      template: '<Index/>',
      components: { Index }
    })
    
    里面需要引入store文件下面的index.js文件,当文件夹下面有index.js文件时,只需写入相关文件路径就能引入index.js文件,在最外层套上store下的index.js模块
##3,在需要向store里面取数据的XX.vue文件。
###1,store.commit();
    store.commit(SHOW_DATA,data);
    提交相关数据

##4,在需要向store取数据的XX.vue文件
###1,computed
    由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在计算属性中返回某个状态:
    computed: {
        count () {
          return store.state.count
        }
      }
    或者
    computed:{
        ...mapState({
            data:state=>state.studentStore.data
            
        })
    },

####mapState 辅助函数
    当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键:
    1.import {mapState} from "vuex"
    引入mapState
    computed:{
        ...mapState({
            data:state=>state.studentStore.data
            
        })
    },




    

原文地址:https://www.cnblogs.com/lk1186578324/p/6951951.html