广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vuex中actions的使用教程详解
  • 476
分享到

Vuex中actions的使用教程详解

2024-04-02 19:04:59 476人浏览 安东尼
摘要

目录简介说明官网actions概述说明特点用法示例测试简介 说明 本文用示例介绍Vuex的五大核心之一:actions。 官网 Action | Vuex api 参考 | Vuex

简介

说明

本文用示例介绍Vuex的五大核心之一:actions。

官网

Action | Vuex

api 参考 | Vuex

actions概述

说明

Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数。

特点

1.异步操作,通过mutations来改变state。

2.不能直接改变state里的数据。

3.包含多个事件回调函数的对象。

4.执行方式:通过执行 commit()来触发 mutation 的调用, 间接更新 state

5.触发方式: 组件中: $store.dispatch(‘action 名称’, data1)

6.可以包含异步代码(例如:定时器, 请求后端接口)。

用法

直接使用

this.$store.dispatch('actions方法名', 具体值)        // 不分模块
this.$store.dispatch('模块名/actions方法名', 具体值) // 分模块

mapActions

import { mapActions } from 'vuex'
export default {
    computed: {
        // 不分模块
        ...mapActions(['actions方法名'])          
 
        // 分模块,不改方法名
        ...mapActions('模块名', ['actions方法名'])
        
        // 分模块,不改方法名
        ...mapActions('模块名',{'新actions方法名': '旧actions方法名'})
    }
}

示例

CounterStore.js

import Vue from 'vue';
import Vuex from 'vuex';
 
Vue.use(Vuex);
const counterStore = new Vuex.Store(
    {
        state: {
            count: 10
        },
 
        getters: {
            doubleCount(state) {
                return state.count * 2;
            }
        },
 
        mutations: {
            increment(state) {
                state.count++;
            },
            decrement(state) {
                state.count--;
            },
            // 带参数
            addNumber(state, param1) {
                state.count += param1;
            },
        },
 
        actions: {
            asyncIncrement(context) {
                console.log('CounterStore=> action: asyncIncrement');
                setTimeout(() => {context.commit('increment')}, 1000)
            },
 
            asyncAddNumber(context, n) {
                console.log('CounterStore=> action: asyncAddNumber');
                setTimeout(() => {context.commit('addNumber', n)}, 1000)
            }
        }
    }
);
 
export default counterStore;

Parent.vue(入口组件)

<template>
  <div class="outer">
    <h3>父组件</h3>
    <component-a></component-a>
    <component-b></component-b>
  </div>
</template>
 
<script>
import ComponentA from "./ComponentA";
import ComponentB from "./ComponentB";
 
export default {
  name: 'Parent',
  components: {ComponentA, ComponentB},
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

ComponentA.vue(异步修改vuex的数据) 

<template>
  <div class="container">
    <h3>ComponentA</h3>
    <button @click="thisAsyncIncrement">异步加1</button>
    <button @click="thisAsyncAddNumber">异步增加指定的数</button>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      cnt: 5
    }
  },
  methods:{
    thisAsyncIncrement() {
      this.$store.dispatch('asyncIncrement')
    },
    thisAsyncAddNumber() {
      this.$store.dispatch('asyncAddNumber', this.cnt)
    }
  }
}
</script>
 
<style scoped>
.container {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

ComponentB.vue(读取vuex的数据) 

<template>
  <div class="container">
    <h3>ComponentB</h3>
    <div>计数器的值:{{thisCount}}</div>
    <div>计数器的2倍:{{thisDoubleCount}}</div>
  </div>
</template>
 
<script>
export default {
  computed:{
    thisCount() {
      return this.$store.state.count;
    },
    thisDoubleCount() {
      return this.$store.getters.doubleCount;
    },
  }
}
</script>
 
<style scoped>
.container {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

路由(router/index.js)

import Vue from 'vue'
import Router from 'vue-router'
import Parent from "../components/Parent";
 
Vue.use(Router)
 
export default new Router({
  routes: [
    {
      path: '/parent',
      name: 'Parent',
      component: Parent,
    }
  ],
})

测试

访问: Http://localhost:8080/#/parent

到此这篇关于Vuex中actions的使用教程详解的文章就介绍到这了,更多相关Vuex actions内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vuex中actions的使用教程详解

本文链接: https://www.lsjlt.com/news/164082.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vuex中actions的使用教程详解
    目录简介说明官网actions概述说明特点用法示例测试简介 说明 本文用示例介绍Vuex的五大核心之一:actions。 官网 Action | Vuex API 参考 | Vuex...
    99+
    2022-11-13
  • 详解Vuex中getters的使用教程
    目录简介说明官网getters概述说明来源用法示例测试简介 说明 本文用示例介绍Vuex的五大核心之一:getters。 官网 Getter | Vuex API 参考 | Vuex...
    99+
    2022-11-13
  • Vuex中getters和actions的使用补充说明
    前置说明 1.Vue2.x 和 Vue3.x区别: 在Vue3.x中, 没有辅助函数. 其他关于Vuex的使用没有区别. 2. 此处只对于Vuex的几个属性中的使...
    99+
    2022-11-12
  • vue中使用vuex的超详细教程
    目录一、适合初学者使用,保存数据以及获取数据二、模块化(适合有部分基础的人)vuex是使用vue中必不可少的一部分,基于父子、兄弟组件,我们传值可能会很方便,但是如果是没有关联的组件...
    99+
    2022-11-13
    vue使用vuex vue vuex
  • vue3.2中的vuex使用详解
    Vuex 中有以下几个核心概念: State:应用程序的状态存储在单一的状态树中,即 State。State 可以通过 store.state 属性访问。Mutation:状态的变化...
    99+
    2023-05-15
    vue3.2使用vuex vue3.2 vuex使用
  • vuex中Modules的使用详解
    目录前言1 、什么是模块Modules2、模块内部参数问题3、模块命名空间问题(1)namespaced: true 使模块成为带命名空间的模块(2)带命名空间的绑定函数的使用4、模...
    99+
    2022-11-13
  • C#中类的使用教程详解
    目录实例化类成员访问赋值方法方法调用方法重载在对类访问使用时,常用到的有访问类的成员、方法。 实例化 在对类进行访问时,需要将类进行实例化。并产生一个对象。可以使用关键字new来实现...
    99+
    2022-11-13
  • SpringCloud中Gateway的使用教程详解
    目录1.基础教程2.将配置放在配置文件里3.放在springcloud里面4.使用服务名而不是IP1.基础教程 pom.xml <parent> ...
    99+
    2022-11-13
    SpringCloud Gateway使用 SpringCloud Gateway
  • Java中JDBC的使用教程详解
    目录概念快速入门步骤代码实现详解各个对象DriverManager:驱动管理对象Connection:数据库连接对象Statement:执行sql的对象ResultSet:结果集对象...
    99+
    2022-11-13
  • MyBatis中OGNL的使用教程详解
    前言本文主要给大家讲如何在MyBatis中使用OGNL的相关内容,分享出来供大家参考学习,感兴趣的朋友们下面来一起看看详细的介绍:如果我们搜索OGNL相关的内容,通常的结果都是和Struts有关的,你肯定搜不到和MyBatis有关的,虽然和...
    99+
    2023-05-31
    mybatis 使用 ognl
  • java中TestNG使用教程详解
    目录一、TestNG介绍二、TestNG安装(基于eclipse+maven)三、TestNG基本使用和运行1、直接运行:2、xml方式运行四、注解说明五、TestNG断言六、Tes...
    99+
    2022-11-12
  • vuex中Getter的用法详解
    前言 Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依...
    99+
    2022-11-12
  • 如何使用Vuex的入门教程
    目录前言一、基本概念初识vuex二、项目场景三、如何使用1、安装2、State初始值3、Getters修饰值4、Mutations修改值5、Actions异步修改值四、总结五、建议前...
    99+
    2022-11-13
  • 详解Android Flutter中SliverAppBar的使用教程
    目录简介SliverAppBar详解SliverAppBar的使用总结简介 对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用...
    99+
    2023-01-31
    Android Flutter SliverAppBar使用 Android SliverAppBar Flutter SliverAppBar
  • 详解supervisor使用教程
    A Process Control System 使用b/s架构、运行在类Unix系统上一个进程监控管理系统它可以使进程以daemon方式运行,并且一直监控进程,在意外退出时能自动重启进程。 安装 Supe...
    99+
    2022-06-04
    详解 教程 supervisor
  • vuex中数据持久化插件vuex-persistedstate使用详解
    目录数据持久化vuex-persistedstate使用安装起步配置使用自定义存储方式想使用cookie同理使用vuex-persistedstate插件遇到的问题数据持久化vuex...
    99+
    2022-11-13
  • Vuex中状态管理器的使用详解
    目录一、Vuex是什么?二、什么时候使用Vuex三、Vuex的核心概念和API四、应用举例五、vuex中各种辅助函数的用法,可以使我们更加方便的运用vuex一、Vuex是什么? Vu...
    99+
    2022-11-13
  • Python中itertools模块的使用教程详解
    目录itertools模块的介绍无限迭代器(Infinite Iterators)组合迭代器(Combinatoric Iterators)有限迭代器(Iterators Termi...
    99+
    2022-11-11
  • 详解Unity中的ShaderGraph入门使用教程
    一,ShaderGraph 简介 简介: Unity2018版本之后推出了一个可编程渲染管线工具ShaderGraph,让我们可以通过可视化界面拖拽来实现着色器的创建和编辑。 官方话...
    99+
    2022-11-12
  • Flutter中数据库的使用教程详解
    在Flutter开发过程中,我门有时候需要对一些数据进行本地的持久化存储,使用sp文件形式虽然也能解决问题,但是有时数据量较大的时候,显然我们文件形式就不太合适了,这时候我们就需要使...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作