Categories
Vue Answers

How to trigger events using Vue.js?

Spread the love

Sometimes, we want to trigger events using Vue.js.

In this article, we’ll look at how to trigger events using Vue.js.

How to trigger events using Vue.js?

To trigger events using Vue.js, we can use refs.

For instance, we write

<template>
  <button type="button" @click="myClickEvent" ref="myBtn">Click Me!</button>
</template>

<script>
export default {
  methods: {
    myClickEvent() {
      const elem = this.$refs.myBtn;
      elem.click();
    },
  },
};
</script>

to assign the myBtn ref to the button.

Then in the myClickEvent method, we get the button with this.$refs.myBtn.

And then we call click on it to click it programmatically.

Conclusion

To trigger events using Vue.js, we can use refs.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *