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
Add a ment  | 

2 Answers 2

Reset to default 8

You 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