TS 2322:类型“(状态:州,例如:编号,例如姓名:string)=>void'不能赋给类型'Mutation ',- Vuex/Typescript

pbgvytdp  于 2023-01-14  发布在  TypeScript
关注(0)|答案(1)|浏览(94)

我目前正在使用vuex和typescript,但我的代码中有这个错误,我不知道如何修复它。TS2322: Type '(state: State, exRep: number, exName: string) => void' is not assignable to type '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"突变处

nue99wik

nue99wik1#

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

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

相关问题