Vue declaration structure array

how do I declare an array of structures in the data of vue?

...
data () {
    return {
        structArray: [],//  1
        
        unit: {id: "", name: ""},   
        structArray2: unit[10]//  2
    }
}
...

as above, what if you want structArray to be an array of structs, such as
[{id: "1arrays, name: "a"}, {id:" 2codes, name: "b"}, {id: "3codes, name: "c"}]
? If
declares it as 1, structArray [0] .id ="1" will have an undefined error, that is, the variable is not initialized. If
uses the method of 2, you need to specify the number of elements in the array, which is not allowed as structArray2: unit [] .
Note that the number of elements in the array is not known at the initial moment, but the element structure {id:"", name:""} is known.

Jul.28,2021

you can take a closer look at the pipe network

data: {
  newTodoText: '',
  visitCount: 0,``
  hideCompletedTodos: false,
  todos: [],
  error: null
}

or

in the component
data: function () {
  return {
    count: 0
  }
}

isn't it a little strange how you write it?
and the vue object has scope after all.
some vue object. StructArray [0] .id ='1'. Have you tried it?


data () {
    return {
        structArray: [],//  1
        unit: {},   
        
    }
}

unit.id = '1'
unit.name = 'a'
structArray.push(unit)

is this not good?


I find this word very interesting. Js itself is weakly typed, and the term struct is basically implemented in a strongly typed language, and js should not give too much expectation to this requirement. If you do want to implement such forced deconstruction assignments at the business logic level, it is recommended that you use computational properties to implement the setter method. For specific implementation, please see the vue tutorial ide/computed.html" rel=" nofollow noreferrer "> setter of https://cn.vuejs.org/v2/guide. calculation attribute

Menu