V
V
Vadim Sutormin2020-05-11 09:17:20
Vue.js
Vadim Sutormin, 2020-05-11 09:17:20

How to dynamically change the name of a called function in a Vue component?

There is a component where there is a handler that calls a function.

Example:

Vue.component('vm-table', {
    props: ['list'],
    template: '<button v-on:click="$root.aaa()">Редактировать</button>'
});


How can I make the name of the called function dynamic, which will be passed from outside, that is, the function is not aaa(), but some other one, which will be passed through the list parameter?
'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Drozdov, 2020-05-11
@Wunder

v-on:click="$root[list]()"

A
Alexander Marchuk, 2020-05-11
@imhvost

<template>
  <button @click="edit()">Редактировать</button>
</template>
<script>
export default {
  props: {
    list: {
      type: Object,
      required: true
    }
  },
  methods: {
    edit () {
      if (this.list.functionName === 'aaa') this.$root.aaa()
    }
  }
}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question