VueX用法快速回顾(适用于有基础的同学)

简介: 【8月更文挑战第20天】VueX用法快速回顾(适用于有基础的同学)

vuex基础结构

代码结构

vuex的完整结构长这样,其包含了state、mutations、actions、modules及getters5个部分。

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
   
   
  state: {
   
   },
  mutations: {
   
   },
  actions:{
   
   },
  modules:{
   
   },
  getters:{
   
   }
})

new Vue({
   
   
  el: '#app',
})

假设我们在上述store内补充一些方法

const store = new Vuex.Store({
   
   
  state: {
   
   
    count:0
  },
  mutations: {
   
   
    add(state){
   
   
      state.count ++
    }
  },
  actions:{
   
   },
  modules:{
   
   },
  getters:{
   
   }
})

现在,我们可以采用store.commit('add') 的方式来更改count的值。
为了在其他vue组件中使用 this.$store 访问,我们可以这么做:

new Vue({
   
   
  el: '#app',
  store: store,
})

项目结构

为了使项目结构更加清晰,我们一般这么定义结构

├─ src
│  ├─ App.vue
│  ├─ main.js
│  └─ store
│     └─ index.js
//  index.js
import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
   
   
  state: {
   
   
    count: 0,
  },
  getters: {
   
   },
  mutations: {
   
   
    add(state) {
   
   
      state.count++;
    },
  },
  actions: {
   
   },
  modules: {
   
   },
});
// main.js
import Vue from "vue";
import App from "./App.vue";
import store from "./store";

Vue.config.productionTip = false;

new Vue({
   
   
  store,
  render: (h) => h(App),
}).$mount("#app");

如果modules比较多,我们还可以在创建一个文件夹用来存放,比如在store下创建moduleA文件夹,我们在moduleA内创建index.js,并写入一些内容:

export default {
   
   
  state: {
   
   },
  getters: {
   
   },
  mutations: {
   
   },
  actions: {
   
   },
};

然后store根目录的index.js可以改写

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

export default new Vuex.Store({
   
   
  state: {
   
   
    count: 0,
  },
  getters: {
   
   },
  mutations: {
   
   
    add(state) {
   
   
      state.count++;
    },
  },
  actions: {
   
   },
  modules: {
   
   
    moduleA: moduleA,
  },
});

state的访问

state的方法有两种,一种是采用this.$store直接访问,另一种是借助mapState 辅助函数。

this.$store访问

在上图中,我们可以看到this.$store上存在state属性,我们可以展开看看
GIF 2023-2-9 10-59-56.gif
很明显,通过this.$store.count可以直接访问到

mapState 辅助函数

import {
   
    mapState } from "vuex";  //引入方法
export default {
   
   
  name: "App",
  computed: {
   
   
    ...mapState(["count"]),        // 注意这里是数组
  },
};

image.png

Mutation的触发

我们知道Mutation中的函数是用来更改state中某个属性的状态的,如下面的代码

export default new Vuex.Store({
   
   
  state: {
   
   
    text: 0,
  },
  mutations: {
   
   
    // mutations函数有两个参数,第一个参数是 state ,第二个参数是传递进来的值
    changeText(state, payload) {
   
   
      state.text = payload;
    },
  },
});

它有两种调用方式:

通过this.$store触发

  methods: {
    // 在函数中直接使用  
    changeBtn1() {
      this.$store.commit("changeText", "你好,世界");
    },
  },

通过mapMutations辅助函数

// 1.引入mapMutations函数
import { mapMutations } from "vuex";
methods: {
  // 2.在methods中使用拓展运算符展开函数
  ...mapMutations(["changeText"]),
  // 3.调用阿含糊
  changeBtn2() {
    this.changeText("鱿鱼须懂个锤子vue");
  },
},

代码演示

<template>
  <div id="app">
    <div>text的内容是: {
  
  { text }}</div>
    <button @click="changeBtn1">this.$store.commit更改</button>
    <button @click="changeBtn2">mapMutations更改</button>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "App",
  computed: {
    ...mapState(["text"]),
  },
  methods: {
    ...mapMutations(["changeText"]),
    changeBtn1() {
      this.$store.commit("changeText", "你好,世界");
    },
    changeBtn2() {
      this.changeText("鱿鱼须懂个锤子vue");
    },
  },
};
</script>

GIF 2023-2-9 11-35-03.gif

Action的触发

Action的触发方式同Mutation,它可以用来执行异步函数

export default new Vuex.Store({
  state: {
    text: 0,
  },
  mutations: {
    // mutations函数有两个参数,第一个参数是 state ,第二个参数是传递进来的值
    changeText(state, payload) {
      state.text = payload;
    },
  },
  actions: {
    laterChange(context, value) {
      setTimeout(() => {
        context.commit("changeText", value);
      }, 1000);
    },
  },
});

通过this.$store触发

  methods: {
    // 在函数中直接使用  
    changeBtn1() {
      this.$store.dispatch("laterChange", "你好,世界");
    },
  },

通过mapActions辅助函数

// 1.引入mapActions函数
import {mapActions } from "vuex";
methods: {
  // 2.在methods中使用拓展运算符展开函数
  ...mapMutations(["laterChange"]),
  // 3.调用阿含糊
  changeBtn2() {
    this.laterChange("鱿鱼须懂个锤子vue");
  },
},

代码演示

<template>
  <div id="app">
    <div>text的内容是: {
  
  { text }}</div>
    <button @click="changeBtn1">this.$store.dispatch更改</button>
    <button @click="changeBtn2">mapActions更改</button>
  </div>
</template>

<script>
import { mapState, mapActions, } from "vuex";
export default {
  name: "App",
  computed: {
    ...mapState(["text"]),
  },
  methods: {
    ...mapActions(["laterChange"]),
    changeBtn1() {
      this.$store.dispatch("laterChange", "你好,世界");
    },
    changeBtn2() {
      this.laterChange("鱿鱼须懂个锤子vue");
    },
  },
};
</script>

GIF 2023-2-9 11-48-30.gif

actions函数的context参数

context有若干参数,如下
image.png

  • 通过dispatch可以直接调用action平级内的函数
  • 通过commit可以调用mutations内的函数
  • 通过getters可以直接访问当前模块内的getters属性值
  • 通过state可以访问当前模块内的state属性值
  • 通过rootState可以访问根节点内的state属性值
  • 通过rootGetters可以访问根节点内的getters属性值

实际开发中,利用解构赋值可以优化代码:

  actions: {
    laterChange({ dispatch, commit }, value) {
      setTimeout(() => {
        commit("changeText", value);
      }, 1000);
    },
  },

Module

当应用变得非常复杂时,store 对象就有可能变得相当臃肿。
为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块。

export default new Vuex.Store({
   
   
  state: {
   
   },
  mutations: {
   
   },
  actions: {
   
   },
  getters: {
   
   },
  modules: {
   
   
    moduleA: {
   
   
      // 是否开启命名空间
      namespaced: true,                
      state: {
   
    count: 0 },
      getters: {
   
   },
      mutations: {
   
   
        add(state, payload) {
   
   
          state.count += payload;
        },
      },
      actions: {
   
   
        sub({
   
    state, commit }, payload) {
   
   
          commit("add", payload);
        },
      },
    },
  },
});

state数据访问

// 方法一
this.$store.state.moduleA.count
// 方法二 需要模块开启命名空间,不开启会报错
computed: {
   
   
  ...mapState("moduleA", ["count"]),
},

不开启命名空间的时候,只能使用方式一,开启后两种方式都可以使用

Mutation的调用

// 方法一
this.$store.commit("moduleA/add", "自定义value");
// 方法二 需要模块开启命名空间,不开启会报错
methods: {
   
   
  ...mapMutations("moduleA", ["add"]),
  changeBtn1() {
   
   
    this.add("自定义value");
  },
},

Action的调用

// 方法一
this.$store.dispatch("moduleA/sub", "自定义value");
// 方法二 需要模块开启命名空间,不开启会报错
methods: {
   
   
  ...mapActions("moduleA", ["sub"]),
  changeBtn1() {
   
   
    this.sub("自定义value");
  },
},
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8082 2
|
11天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10346 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1564 5
|
9天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2709 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2465 0