vuex学习笔记

安装

npm i vuex@版本

使用方式

//main.js
import App from "./App.vue";
import Vue from "vue";
//引入
import Vuex from "vuex";
//注册
Vue.use(Vuex);
//实例化vuex
const store = new Vuex.Store({
//写配置
//存放公用数据
state: {
    a: "hello store",
  },
});
new Vue({
  //挂载
  store,
  render: (h) => h(App),
}).$mount("#app");
<template>
  <div></div>
</template>

<script>
export default {
  created() {
    //如果成功打印一个对象,就说明成功了
    console.log('stroe: ',this.$store);
    console.log("state:a ", this.$store.state.a);
  }
};
</script>

state

概念

专门用来声明全局变量的地方

基本使用

配置state

const store = new Vuex.Store({
state: {
    a: "hello store",
  },
});

调用公共数据

<template>
  <div>{{$store.state.a}}</div>
</template>

优化使用

配置写法无变化

const store = new Vuex.Store({
state: {
    a: "hello store",
  },
});

调用时使用计算属性

<template>
  <div>{{ a }}</div>
</template>

<script>
export default {
  computed: {
    a() {
      return this.$store.state.a;
    },
  },
};
</script>

辅助函数的使用(mapState的使用)

  1. 配置不变

  2. 在组件中调用

    <template>
      <div>{{ a }}</div>
    </template>
    
    <script>
    //引入
    import { mapState } from "vuex";
    export default {
      data() {
        return {};
      },
      computed: {
        //通过扩展运算符把辅助函数数据的返回值结构到计算属性
        //中,从而获取state中的变量a
        ...mapState(["a"]),
      },
    };
    </script>
    
    

mutations

概念

专门用来修改state中的变量的对象(属性)

基本使用

1.写配置

state: {
    a: "hello store",
    count: 0,
  },
  mutations: {
    //state为固定形参
    addCount(state, num) {
      state.count += num;
    },
  },

2.组件调用

<template>
  <div>
    {{ count }}
    <button @click="$store.commit('addCount', 5)">点击我修改count值</button>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["a", "count"]),
  },
};
</script>

优化使用

1.配置不变

2.组件的调用

<template>
  <div>
    {{ count }}
    <!-- 通过事件处理来调用 -->
    <button @click="changeCount(5)">点击我修改count值</button>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {};
  },
 
  computed: {
    ...mapState(["a", "count"]),
  },

  methods: {
    changeCount(num) {
      this.$store.commit("addCount", num);
    },
  },
};
</script>

辅助函数(mapMutations)

1.配置项不变

2.组件的调用

<template>
  <div>
    {{ count }}
    <button @click="addCount(5)">点击我修改count值</button>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {};
  },

  computed: {
    ...mapState(["a", "count"]),
  },

  methods: {
    //mutations的辅助函数要写在methods
    ...mapMutations(["addCount"]),
  },
};
</script>

actions

概念

通过异步操作,调用mutations中的函数,从而改变state中的变量

基本使用

1.写配置

const store = new Vuex.Store({
  //写配置
  state: {
    a: "hello store",
    count: 0,
  },
  mutations: {
    addCount(state, num) {
      state.count += num;
    },
  },
  actions: {
    addAsyncCount(store, num) {
      //store内置的参数,指的是Vuex.Store的实例化对象store
      setTimeout(() => {
        store.commit("addCount", num);
      }, 1000);
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ count }}
    <button @click="$store.dispatch('addAsyncCount', 5)">
      点击我修改count值
    </button>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["a", "count"]),
  },
};
</script>

优化使用

1.配置项不变

2.组件的调用

<template>
  <div>
    {{ count }}
    <button @click="ayncChangeCount(5)">点击我修改count值</button>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["a", "count"]),
  },

  methods: {
    ayncChangeCount(num) {
      this.$store.dispatch("addAsyncCount", num);
    },
  },
};
</script>

辅助函数

1.配置项不变

2.组件的调用

<template>
  <div>
    {{ count }}
    <!-- 直接使用 -->
    <button @click="addAsyncCount(5)">点击我修改count值</button>
  </div>
</template>

<script>
//引入mapActions
import { mapState, mapActions } from "vuex";
export default {
  computed: {
    ...mapState(["a", "count"]),
  },

  methods: {
    //解构
    ...mapActions(["addAsyncCount"]),
  },
};
</script>

getters

概念

除了state可以声明全局变量之外,还可以利用getters声明全局变量,只不过getters声明的变量是从state派生出来的,getters就相当于Vue中的computed属性

基本使用

1.配置

const store = new Vuex.Store({
  state: {
    list: [1, 2, 3, 4, 5],
  },
  //写配置
  getters: {
    filterList: (state) => {
      return state.list.filter((item) => item >= 3);
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ $store.getters.filterList }}
  </div>
</template>

<script>
export default {
};
</script>

优化使用

辅助函数

1.配置不变

2.组件的调用

<template>
  <div>
    {{ countHandle }}
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["countHandle"]),
  },
};
</script>

module

概念

为了能够分模块管理我们的全局变量,这样有利于项目的后期维护

使用方式

1.写配置

const store = new Vuex.Store({
  modules: {
    user: {
      state: {
        token: "123",
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    <!-- $store.state.模块名.属性名 -->
    {{ $store.state.user.token }}
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
};
</script>

简化使用

1.配置项

const store = new Vuex.Store({
  getters: {
    token(state) {
      return state.user.token;
    },
  },
  },
  modules: {
    user: {
      state: {
        token: "123",
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ token }}
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["token"]),
  },
};
</script>

注意

  • 默认情况下,mutations,actions,getters声明的变量或者函数都是直接挂载到全局的命名空间中的
  • 在模块名内添加namespace属性可以使mutations,actions,getters存在局部空间内

module中的局部state

1.配置

const store = new Vuex.Store({
  getters: {
    token(state) {
      return state.user.token;
    },
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        token: "123",
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ token }}
    <button></button>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["token"]),
  },
};
</script>

module中的局部mutations

1.写配置

const store = new Vuex.Store({
  getters: {
    token(state) {
      return state.user.token;
    },
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        token: "123",
      },
      mutations: {
        changeToken(state, string) {
          state.token = string;
        },
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ token }}
    <button @click="changeToken('13456')">点击修改</button>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";
export default {
  data() {
    return {};
  },

  computed: {
    ...mapGetters(["token"]),
  },
  methods: {
    //注意
    ...mapMutations("user", ["changeToken"]),
  },
};
</script>
<style lang="scss" scoped></style>

module中的局部actions

1.配置

const store = new Vuex.Store({
  getters: {
    token(state) {
      return state.user.token;
    },
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        token: "123",
      },
      mutations: {
        changeToken(state, string) {
          state.token = string;
        },
      },
      actions: {
        asyncChangeToken(store, string) {
          setTimeout(() => {
            store.commit("changeToken", string);
          }, 500);
        },
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ token }}
    <button @click="asyncChangeToken('13456')">点击修改</button>
  </div>
</template>

<script>
import { mapGetters, mapMutations, mapActions } from "vuex";
export default {
  computed: {
    ...mapGetters(["token"]),
  },
  methods: {
    ...mapMutations("user", ["changeToken"]),
    ...mapActions("user", ["asyncChangeToken"]),
  },
};
</script>

module中的局部getters

1.配置

const store = new Vuex.Store({
  getters: {
    token(state) {
      return state.user.token;
    },
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        token: "123",
      },
      getters: {
        tokenGetter(state) {
          return state.token + "789";
        },
      },
    },
  },
});

2.组件的调用

<template>
  <div>
    {{ tokenGetter }}
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters("user", ["tokenGetter"]),
  },
};
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值