Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS - Pass variable to this.{variable}

I would like to have a property dynamically accessed with a variable.

For example, I have :

data(){
    redsection : '',
    bluesection : '',
},
methods(){
     changeColor(color, val){
          this.{color+"section"} = val;
     }
}

And what I'm trying to do it have a function in the view:

<button @click="changeColor("blue", "bar")>blue</button>
<button @click="changeColor("red", "bar")>blue</button>
like image 378
ayame Avatar asked Aug 17 '26 19:08

ayame


1 Answers

Use an indexer.

this[color+"section"] = val;

Here is an example.

like image 137
Bert Avatar answered Aug 19 '26 11:08

Bert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!