[Vuejs]-Vue.js: Is there a way to have @submit.prevent on all the forms of a project?

1👍

You can create a simple (probably functional) component and use this instead of the normal <form>.

// BaseForm.vue

<template>
  <form @submit.prevent="onSubmit">
    <slot />
  </form>
</template>

<script>
export default {
  props: {
    onSubmit: {
      type: Function,
      required: true,
    }
  }
}
</script>

You won’t really save a lot of code, but you won’t have to think about it anymore. You could include the basic <button type=submit></button> in here as well.

Leave a comment