VUEX

作者: 小碗吃不了 | 来源:发表于2019-11-21 10:06 被阅读0次
安装
  • HTML 中使用 script 标签引入

    <script src="vue.js"></script>
    <script src="vuex.js"></script>
    
  • vue项目中使用 npm 下载安装(需要安装 Node 环境)

    // 下载
    npm install vuex --save
    
    // 安装
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
Vue 项目中 vuex 的使用
  • store.js

    import Vuex from 'vuex'
    Vue.use(Vuex)
    export default Vue.prototype.$store = new Vuex.Store({//声明了全局变量
      state:{},
      getters:{},
      mutations:{},
      action:{}
     })
    
  • main.js

    import store from './store';
    new Vue({
      el: '#app',
      store,
      components: { App },
      template: '<App/>'
    })
    
默认的五种基本的对象
  • state:存储状态(变量),组件中使用this.$store.state.count

     state: {
        count: 0
    }
    
  • getters:对数据获取之前的再次编译,即state的计算属性,组件中使用 $sotre.getters.fun()

    getters: {
        getterCount(state, n = 0) {
            return (state.count += n)
      }
     }
    
  • mutations:修改状态,并且是同步的。组件中使用$store.commit('',params)

     mutations: {
      mutationsAddCount(state, n = 0) {
        return (state.count += n)
      },
      mutationsReduceCount(state, n = 0) {
        return (state.count -= n)
      }
    }
    
    //在组件的自定义事件内调用
    this.$store.commit('mutationsAddCount',n);
    this.$store.commit('mutationsReduceCount',n);
    
  • actions:异步操作。在组件中使用是$store.dispath(''")

    actions: {
      actionsAddCount(context, n = 0) {
        console.log(context)
        return context.commit('mutationsAddCount', n)
    },
      actionsReduceCount({ commit }, n = 0) {
        return commit('mutationsReduceCount', n)
      }
    }
    
    //在组件内自定义方法使用
    this.$store.dispatch('actionsAddCount',n);
    this.$store.dispatch('actionsReduceCount',n);
    
  • modules:store的子模块,为了开发大型项目,方便状态管理而使用的

    //  新建module1.js
    export default {
     // namespaced:true,  //开启namespace:true,该模块就成为命名空间模块了
      state:{
        str: 'store_a',//调用this.$store.state.mod_a.str
    },
      getters:{...},
      mutations:{...},
      actions:{...}
    }
    
    //在store引入并配置
    import ModA from './module1'
    export default new Vuex.Store({
        modules: {
            mod_a: ModA
      }
    })
    
Vuex 和全局对象区别
  • Vuex 的状态存储是响应式的

  • 不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation

  • 使用Vue.prototype实现全局变量

    //main.js
    Vue.prototype.global={a:0}
    //组件内调用,一个组件内改变该值,全局都改变
    this.global.a
    
  • 在根节点中创建变量来实现全局变量

     //在main.js
    new Vue({
      ……
      data(){
        return{
            a:1
        }
      },
     ……
    })
    //在子组件调用
    $root.a

相关文章

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

  • vuex配置

    vuex配置 目录 vuex的五个核心 配置vuex vuex持久化 一、vuex五个核心概念 State 定义状...

  • Vuex

    安装Vuex cnpm install vuex --save-dev Vuex是什么? 这是[Vuex的官网](...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • vuex+axios 的开发流程记录

    相关文档 vuex: https://vuex.vuejs.org/zh/ 是否有必要使用vuex vuex是vu...

  • 2019-06-07

    import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)...

  • 配置 vuex 和 vuex 本地持久化

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

  • vuex

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

网友评论

      本文标题:VUEX

      本文链接:https://www.haomeiwen.com/subject/sffaictx.html