admin管理员组文章数量:1279090
I have ponent in vue 2 which is written in 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'
});
And I want to add new item to existed array userfilterresults but I've got error: Argument type {..} is not assignable to parameter of type never How I can add new item to the array?
I have ponent in vue 2 which is written in 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'
});
And I want to add new item to existed array userfilterresults but I've got error: Argument type {..} is not assignable to parameter of type never How I can add new item to the array?
Share Improve this question edited Dec 29, 2017 at 23:20 Robert asked Dec 29, 2017 at 22:34 RobertRobert 2,70112 gold badges69 silver badges103 bronze badges 3- response.data is an array? – Jonathan Dion Commented Dec 29, 2017 at 23:24
- yes, response.data is the array of objects: { id : '-1', userid : '1', name : 'newtab' } – Robert Commented Dec 29, 2017 at 23:36
- the problem is that at the begin the array userfilterresults is empty – Robert Commented Dec 29, 2017 at 23:37
2 Answers
Reset to default 8You need to declare a type for userfilterresults
.
By default, for let x = []
, type of x
will be never[]
.
You need to specify it explicitly or mark it as any[]
. e.g.
let x: any[] = []
let y: CustomType[] = []
I'm not familiar with the typings of Vue, but that is the underlying reason.
Try
data: {
userfilterresults: [] as any[]
}
and see if that helps.
userfilterresults:any= [] ;
addtab() {
var obj={ id : '-1', userid : '1', name : 'newtab'
} }
this.userfilterresults.push(obj);
本文标签: javascriptTypescript how to add new item to empty array of objectsStack Overflow
版权声明:本文标题:javascript - Typescript how to add new item to empty array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282983a2370114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论