R
R
Romi2021-04-26 12:33:17
Vue.js
Romi, 2021-04-26 12:33:17

How to generate variable name dynamically?

Let's say I have a method:

clickHandle: function() {
                console.log(this.someVar);
            }


I also have someVar in the data object.

and I call the method from the template: and everything is fine. But what if I want to pass different variables to clickHandle() so that it is displayed depending on the parameter, not only but also How is it better to do this? Please write a line of code instead of a thousand explanations :) Thank you. As an option (I even need it more): for example here: I somehow want to pass not even the name of the variable "someOtherVar" itself, but its PART, that is: "someOther" (string) and somehow substitute it so that the call is as expected: I didn't explain very confusingly? :)

<button @click="clickHandle()"></button>



console.log(this.someVar);


console.log(this.someOtherVar);






console.log(this.someOtherVar);




console.log(this.someOtherVar);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2021-04-26
@romicohen

<button @click="clickHandle('varName')"></button>

clickHandle(varName) {
    console.log(this[varName]);
    console.log(this.$data[varName]); // Если только из data надо выводить
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question