2

Here is an element with a @click handler:

<button @click="doSomething">doSomething</button>

Inside this element, how can we programatically change the value of doSomething with doSomethingElse?

1 Answer 1

1

Store the function that you want as the handler in a variable.

In this example, handler is the click handler for the first button. Clicking the second and third buttons changes the function that handler is set to.

new Vue({
  el: '#app',
  data() {
    return {
      handler: this.doSomething,
    }
  },
  methods: {
    doSomething() {
      console.log('something')
    },
    doSomethingElse() {
      console.log('something else')
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app">
  <button @click="handler">Fire Click Handler</button>
  <button @click="handler = doSomething">Make Handler Do Something</button>
  <button @click="handler = doSomethingElse">Make Handler Do Something Else</button>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.