save method name in variable
-
export default { data () { return { methodToCall: '' } }, methods: { someWhereInCode () { this.methodToCall = 'myMethod1' }, someFunction () { // execute myMethod1 or myMethod2 this[methodToCall]() doesn't work this.methodToCall() doesn't work }, myMethod1 () { }, myMethod2 () { } } }
Hi
Is it possible to call a method from a variable name ?
The method to execute depends on the return of a function.
I tried the way I post in the example code and also declaring a variable on the <script>. No success.Is there a way of doing this ?
Tks
-
@Pedro use
this[this.methodToCall]()
-
It worked and helped me a lot
Thanks -
@Pedro You can also use:
const methodToCall = this.methodToCall this[methodToCall]()
if you didn’t like the double
this