what is Vuex ? 什么是Vuex

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式

state属性

在store文件在创建 index.js文件

/**1.注入插件 Vuex*/

import  Vue from "vue";
import Vuex from "vuex"
Vue.use(Vuex);

/** 2创建一个 vuex 实例 仓库 store*/
 var store = new Vuex.Store({
    /**state公共数据 */
    state:{
        count:0
    }

});
 export  default store

在App.vue中使用

import {mapState} from "vuex";
  /**  步骤 3实例注入 vuex 仓库 store*/
  export default {
    store:store,
    components: {About, Home, JieJie, GeGe},
    mounted(){
      console.log(this.$store.state.count);
    },
    /** 助手函数 */
    // computed:mapState(['count'])
    computed:mapState({name:'count'})
  }

助手函数 :mapState

*属性扩散(扩张)浅拷贝

var obj= {a:1,b:2}
var myobj = {...obj,c:3 }
  console.log(myobj); 
//{a: 1, b: 2, c: 3}

如果组件要改变state的数据 都是要同过 commit方法 触发Vuex.Store中的 mutations 方法来执行的 .

methods:{
      add(){
        this.$store.commit('oAdd',3)
      }

在Vuex.Store 的mutations写一个函数

 mutations:{
        oAdd(state,payload){
            state.count+=payload
        }
    }

改变state 的数据 不可以越级

助手函数 mapMutations

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

推荐阅读更多精彩内容

  • 姓名:岳沁 学号:17101223458 转载自:http://blog.csdn.net/h5_queensty...
    丘之心阅读 2,158评论 0 1
  • Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件...
    萧玄辞阅读 3,154评论 0 6
  • 1.什么是状态: 可以理解为在data中的属性需要分享给其他vue组件使用的部分,就叫做状态。简单的说就是data...
    JLong阅读 1,138评论 0 0
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 397评论 0 0
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 740评论 0 3