typescript - TS2322 : Type '(state: State, exRep: number, exName: string) => void' is not assignable to type 'Mutation ' .-Vuex/Typescript

标签 typescript vue.js vuex

我目前正在使用 vuex 和 typescript,但我的代码中有此错误,我不知道如何修复它。 错误:TS2322:类型“(state: State, exRep: number, exName: string) => void”不可分配给类型“Mutation”

我的代码:

import { createStore } from "vuex";
import {State} from "./Types";

const store = createStore<State>({
    state() {
        return {

            exercices: []

        }
    },
    mutations: {
        addEx(state: State, exRep: number, exName: string) {
            const exId = checkid()
            state.exercices.push({ "rep": exRep, "name": exName, "id": exId })
        },
        removeEx(state: State, exoId: Number) {
            console.log(exoId)
            state.exercices = state.exercices.filter(obj => obj.id !== exoId)
        }
    }
})
export default store

错误出现在“addEx”突变处

最佳答案

变异函数只有 2 个参数,如果它们是可变参数是不切实际的。第二个参数是可选的有效负载,如果有多个参数,它应该是一个对象:

addEx(state: State, { exRep, exName }: { exRep: number, exName: string }) {
  ...

关于typescript - TS2322 : Type '(state: State, exRep: number, exName: string) => void' is not assignable to type 'Mutation ' .-Vuex/Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75113044/

相关文章:

javascript - 如何在VueJS中传递props?

javascript - 如果在其字段中包含 SQL 语句,则验证对象

javascript - 如何使用 qunit 在 TypeScript 中将函数参数声明为 "QUnit"类型?

python - 如何将本地 API 与在不同端口提供服务的 Vue 应用程序一起使用?

vue.js - 如何将自定义图像添加到 map 框标记?

javascript - 如何使 vuex getter 响应式

javascript - 动态添加的脚本标签的加载顺序

angular - 延迟加载获取当前路由模块

javascript - Vue.js - Bootstrap Vue Popover 解释 HTML

javascript - Vuex 不更新使用 mapState 映射的计算变量