欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vuex

程序员文章站 2024-02-28 13:25:40
...

vuex是一个专为vue.js应用程序中管理状态。
1.每个组件中的data都称为状态
核心概念
1.State 唯一的数据源【载体】–data
1.1单一的状态树

2.Getters
3.Mutations 唯一提交改变状态 【同步】
4.Actions 提交的是Mutations 【可以包含任意异步操作】
5.Modules 分模块

const store = new Vuex.Store({
    state:{
        count:10,
        name:'min'
    },
    mutations:{
        increment(state,num){
            state.count = num;
        },
        updateName(state,userName){
            state.name = userName;
        }
    },
    actions:{
        updateNameAction(context,name){
            context.commit("updateName",name);//等同于 this.$store.commit("updateName","tanMin")
        }
    },
    getters: {
        doubleCity (state) {
          return state.count + '' + state.name
        }
  }
});

//获取值  this.$store.state.name
//更新值  this.$store.commit("updateName","tanMin");
// actions更新至 this.$route.dispatch("updateNameAction","tanMin");

vuex