javascript - vue - 如何将回调传递给 vuex 操作
问题描述
我正在考虑如何将回调传递给 vuex 操作
我尝试了下面的代码但没有工作。代码在我触发之前运行
src/store/modules/web3.module.js
import Web3 from "web3";
const state = {};
const getters = {};
const mutations = {};
const actions = {
async test(context, confirmCallback, rejectCallback) {
confirmCallback();
rejectCallback();
}
};
export default {
state,
getters,
actions,
mutations
};
应用程序.vue
<template>
<div id="app"></div>
</template>
<script>
import { mapActions } from "vuex";
export default {
name: "App",
methods: {
...mapActions(["test"]),
onModalOpen() {
console.log("open");
},
onModalClose() {
console.log("Close");
},
},
async created() {
let result = await this.test({
confirmCallback: this.onModalOpen(),
rejectCallback: this.onModalClose(),
});
},
};
</script>
解决方案
问题发生在两个地方:
- 您商店中的有效负载语法错误
()
将函数传递给对象时,您将在末尾触发函数:
解决有效载荷问题
一个动作有 2 个参数,首先是上下文,它是一个包含状态、突变等的对象。然后是有效负载,它也是一个对象
const actions = {
async test(context, {confirmCallback, rejectCallback}) {
confirmCallback();
rejectCallback();
}
}
解决 decleration 问题
要解决 decleration 问题,只需删除()
末尾的 ,如下所示:
async created() {
let result = await this.test({
confirmCallback: this.onModalOpen,
rejectCallback: this.onModalClose,
});
},
推荐阅读
- encryption - 这个登录到我的 rest-api 服务器的过程是否有点安全?
- javascript - ReactJS 重新渲染不适用于 CSS
- c# - 我无法在 Windows 10 的 Kiosk 模式下运行我的 UWP c# 应用程序
- visual-studio-code - 如何添加第三方源文件(递归)不是为了构建,而只是为了使用 CMake 在 vscode 中查看定义?
- c++ - 不能在 UE4 中将 BP 类包含到 CPP 类
- tsql - 是否有开箱即用的功能来按文化转换数字?
- java - 如何了解您的应用程序是否已在 Java 中的多线程消费者-生产者场景中完成计算
- configuration - 用于基于时间的模拟的参数存储
- arrays - 从数据透视表中提取值的公式
- php - 是否可以在未经身份验证的路由上使用 Auth::user() ?