如何将对象数组添加到vuejs中的数据?

时间:2019-08-23 04:46:25

标签: vue.js

请在vue上使用。$ set数据提供帮助,在阅读vuejs.org的文档后,当我的数据已经为null时,我还没有意识到$ set中的索引必须是什么,并且我还需要编写语法。请在此特定示例中提供设置数据的语法需求。谢谢。

//in vue js components:
data() {
    return {
      columnDefs: null
    };
computed: {
    onLoadColumns() {
      // some logic here (manipulate received props from parent)
      // logic bring this [{},{},{}]
      // sth = [{},{},{}]

      // now want to .$set() , (or maybe $add()) that array to data obj for reactivity.

      // what syntax look like? this below didn't work.
      this.$set(this.columnsDef, 0, sth);
    }

1 个答案:

答案 0 :(得分:0)

您有一个错字:应为this.$set(this.columnDef, 0, sth);。不是this.columnsDef-没有 s

还尝试使用columnDef中的[]来初始化data

data() {
  return {
    columnDefs: []
  }
},

或在分配零索引之前立即设置空数组:

this.columnDef = []
this.$set(this.columnDef, 0, sth)