一、this.$store.commit()

直接派发一个事件给mutations

  1. //.vue文件
  2. export default {
  3. name: 'home',
  4. methods:{
  5. handleClick(){
  6. this.$store.commit("changeCity")
  7. }
  8. }
  9. }
  1. //store/index.js
  2. mutations:{
  3. changeCity(state){
  4. state.city="成都"
  5. }
  6. }

二、mapState

  1. //store.js
  2. export default new Vuex.Store({
  3. state: {
  4. city:"武汉",
  5. name:"程超",
  6. age:20,
  7. sex:"男"
  8. }
  9. })
  1. <template>
  2. <div class="home">
  3. <h2 >{{city}}</h2>
  4. <h2 >{{age}}</h2>
  5. </div>
  6. </template>
  7. <script>
  8. import {mapState} from 'vuex';
  9. export default {
  10. name: 'home',
  11. computed:{
  12. ...mapState(['city','name','age','sex'])
  13. }
  14. }
  15. </script>

三、mapActions

映射actions中的事件

  1. //store/index.js
  2. export default new Vuex.Store({
  3. state: {
  4. count:0
  5. },
  6. /* 异步操作,处理一些复杂的业务逻辑 */
  7. actions: {
  8. increase(ctx){
  9. ctx.commit("myIncrease")
  10. },
  11. decrease(ctx){
  12. ctx.commit("myDecrease")
  13. }
  14. },
  15. /* 简单的更改状态(state)的逻辑*/
  16. mutations: {
  17. myIncrease(state){
  18. state.count+=1;
  19. },
  20. myDecrease(state){
  21. state.count-=1;
  22. }
  23. },
  24. })
  1. //.vue文件
  2. <template>
  3. <div class="home">
  4. <h2>{{this.$store.state.count}}</h2>
  5. <button @click="increase">增加</button>
  6. <button @click="decrease">减少</button>
  7. </div>
  8. </template>
  9. <script>
  10. import {mapActions} from 'vuex';
  11. export default {
  12. name: 'home',
  13. methods:{
  14. ...mapActions(['increase','decrease']),
  15. }
  16. }
  17. </script>

四、mapMutations

  1. //.vue
  2. <template>
  3. <div class="home">
  4. <h2>{{this.$store.state.count}}</h2>
  5. <button @click="myIncrease">增加</button>
  6. <button @click="myDecrease">减少</button>
  7. </div>
  8. </template>
  9. <script>
  10. import {mapMutations} from 'vuex';
  11. export default {
  12. name: 'home',
  13. methods:{
  14. ...mapMutations(['myIncrease','myDecrease'])
  15. }
  16. }
  17. </script>

五、getters,mapGetters

  1. //store/index.js
  2. /* 对state中的数据进行再次处理 */
  3. getters:{
  4. myCount(state){
  5. return "当前的数量:"+state.count;
  6. }
  7. }
  1. //mapGetters 将 store 中的 getter 映射到局部计算属性
  2. import {mapGetters} from 'vuex';
  3. export default {
  4. name: 'home',
  5. computed:{
  6. ...mapGetters(['myCount'])
  7. }
  8. }

六、modules

作用:当store对象非常复杂的时候,我们可以根据将其拆分成不同的模块

6-1 新建modules文件夹,拆分module

vuex详细知识 - 图1

  1. //user.js
  2. const user = {
  3. state: {
  4. name:"程超"
  5. },
  6. mutations:{},
  7. actions: { },
  8. getters: { }
  9. }
  10. export default user;
  1. //info.js
  2. const info = {
  3. state: {
  4. tips:12,
  5. news:15
  6. },
  7. mutations: {
  8. add(state){
  9. state.tips++;
  10. },
  11. reduce(state){
  12. state.tips--;
  13. }
  14. },
  15. actions: { },
  16. getters: { }
  17. }
  18. export default info;
  1. //index.js
  2. export default new Vuex.Store({
  3. modules:{
  4. user,
  5. info
  6. }
  7. })

6-2 使用数据

  1. <template>
  2. <div class="home">
  3. <h2>首页</h2>
  4. <h3>{{tips}}</h3>
  5. <h4>{{news}}</h4>
  6. <h5>{{name}}</h5>
  7. <button @click="add">增加</button>
  8. <button @click="reduce">减少</button>
  9. </div>
  10. </template>
  11. <script>
  12. import { mapState,mapMutations } from "vuex";
  13. export default {
  14. name: "home",
  15. computed: {
  16. ...mapState({
  17. tips: state => state.info.tips,
  18. news: state => state.info.news,
  19. name: state => state.user.name
  20. })
  21. },
  22. methods:{
  23. ...mapMutations(['add','reduce'])
  24. }
  25. };
  26. </script>

七、vuex,axios拦截器实现Loading

7-1 main.js设置axios拦截器

npm i axios

  1. Vue.prototype.axios = axios;
  2. axios.interceptors.request.use(function (config) {
  3. // Do something before request is sent
  4. store.state.isLoaing = true;
  5. return config;
  6. });
  7. axios.interceptors.response.use(function (response) {
  8. // Do something with response data
  9. store.state.isLoaing = false;
  10. return response;
  11. });

7-2 Vuex state属性中设置isLoading

  1. //store/index.js
  2. export default new Vuex.Store({
  3. state:{
  4. isLoaing:true
  5. }
  6. })

7-3 设置Loading

  1. //.vue
  2. <p v-if="this.$store.state.isLoaing">加载中</p>