Vuex中actions的使用教程详解
作者:IT利刃出鞘 发布时间:2024-04-30 08:45:29
标签:Vuex,actions
简介
说明
本文用示例介绍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
来源:https://blog.csdn.net/feiying0canglang/article/details/122725144


猜你喜欢
- 软件版本:apache:Apache 2.4.6 Win64 PHP:PHP 5.5 VC11 x64 Non Thr
- Python中的字典一、字典的特点二、创建字典创建字典用大括号表示dict1={'a':3,'b':4,
- 问一下谁知道如何用 javascript 获取硬盘信息1.获得硬盘当前有几个盘符.2.每个盘符的 大小,已经使用的大小,和没有使用的大小原理
- golang.org/x包放到了https://github.com/golang/text中,下载时需要先在本地建立golang.org/
- 使用字符串第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处
- 速查表是帮你记住东西的有效工具。Web设计师和开发者经常使用的快捷键简表会使他们在网上的工作效率大大提高。事实上,速查表就是来帮助我们把日常
- 许可和分发权限Access 2003 Developer Extensions随附的许可协议简化了解决方案或代码段的分发过程。这些协议包括免
- __isset() – 在对类中属性或者非类中属性使用isset()方法的时候如果没有或者非公有属性,则自动执行__isset(
- 一. 介绍一个计数器工具提供快速和方便的计数,Counter是一个dict的子类,用于计数可哈希对象。它是一个集合,元素像字典键(key)一
- 文件名字处理文件名字得看业务要求。不需要保留原始名字,则随机生成名字,拼接上白名单校验过的后缀即可。反之要谨慎处理://允许上传的后缀白名单
- 先前我们讲的都是“线性结构”,他的特征就是“一个节点最多有一个”前驱“和一个”后继“。那么我们今天讲的树会是怎样的呢?我们可以对”线性结构“
- 前言在pandas模块中,通常我们都需要对类型为DataFrame的数据进行操作,其中最为常见的操作便是拼接了。比如我们将两个Excel表格
- 本文实例讲述了python采集百度百科的方法。分享给大家供大家参考。具体如下:#!/usr/bin/python# -*- coding:
- 如下所示:import matplotlib.pyplot as pltimport numpy as npdef readfile(fil
- 如下所示:#提取目录下所有图片,更改尺寸后保存到另一目录from PIL import Imageimport os.pathimport
- 对于经常需要表格头部不东,而列表可以滚动,多用于数据比较多的情况,方便查看<!DOCTYPE HTML PUBLIC "-/
- var声明变量的作用域限制在其声明位置的上下文中var x = 0; // x是全局变量,并且赋值为0。console.log(typeof
- 分页是每一个程序需要去理解的东西,学习过的几门语言中我发现分页原理都是一样的,下面为php初学者分析一下php分页实现与最后面补充了一个超级
- 如下所示:ffmpeg中文文档:http://linux.51yip.com/search/ffmpegffmpeg -i test_bao
- 下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文。第一种方式:CONTENT