我目前正在使用 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/