使用方法将数据分配给 vue 数据变量动态

two*_*oam 3 javascript vue.js

如何将数据分配给动态变量。

我想要实现的是:

我的 vue 数据变量:

test: '',
Run Code Online (Sandbox Code Playgroud)

添加点击

<div @click="assignData(test)"
Run Code Online (Sandbox Code Playgroud)

方法:

assignData(value) {
   // Now I don't want this
   this.test = 'lorem..';

   // Instead I want something like this
  value = 'new value for test';

  // Or
  this.value = 'new value for test';
}
Run Code Online (Sandbox Code Playgroud)

现在显然这行不通,但我希望你能明白。

我需要更改许多变量,我不想像这样在函数中添加所有变量

assignData(value) {

   this.test = 'lorem..';
   this.anotherVar = 'ipsum';
   this.newTest = '....';       
}
Run Code Online (Sandbox Code Playgroud)

Ber*_*gur 5

您可以使用该函数发送要更改的属性。

<div @click="assignData('test', 'new value for test)">
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中

 assignData(prop,val) {
        this[prop] = val;
      console.log(this.test);
    }
Run Code Online (Sandbox Code Playgroud)

这是 jsfiddle 的一个片段:https ://jsfiddle.net/49gptnad/6447/