代码之家  ›  专栏  ›  技术社区  ›  asanas

使用变量标识数据对象

  •  1
  • asanas  · 技术社区  · 6 年前

    我在用VueJS。假设我有如下数据:

    data () {
     return {
       status: {
         a: false,
         b: false,
         c: false
       }
    }
    

    method: {
      changeStatus: {
        this.status.b = true
      }
    }
    

    但是,我的要求是我有一个变量,其中包含关于哪些状态需要更改的信息。例如:

    let target = c
    

    所以,在我的方法中,我想改变c的状态,即。, this.status.c = true target )以我的数据中的正确数据为目标?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Dan    6 年前

    可以使用括号表示法: this.status['c'] this.status.c

    然后可以替换文字属性名称字符串 'c' 用一个变量。例如

    method: {
        changeStatus: function(target, value){
            this.status[target] = value;
        }
    }