我在 vue 2 中有一个组件,它是用 typescript 编写的:
data: {
userfilterresults: []
},
mounted() {
axios.get("/Tasks/GetTasks")
.then(response => {
this.userfilterresults = response.data;
});
},
methods: {
addtab() {
// this push bellow is the reason of the error:
this.userfilterresults.push({
id : '-1',
userid : '1',
name : 'newtab'
});
我想向现有数组 userfilterresults 添加新项,但出现错误:Argument type {..} is not assignable to parameter of type never 如何向数组添加新项?
最佳答案
您需要为 userfilterresults
声明一个类型。
默认情况下,对于 let x = []
,x
的类型将为 never[]
。
您需要明确指定它或将其标记为any[]
。例如
let x: any[] = []
let y: CustomType[] = []
我不熟悉 Vue 的类型,但这是根本原因。
尝试
data: {
userfilterresults: [] as any[]
}
看看是否有帮助。
关于javascript - typescript 如何将新项目添加到空对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029149/