Search code examples
javascriptformsvue.jsmultipartform-data

Vue Js : form-data Event


Hello there #Vue.js2 I'm encountering a problem on creating a function called in a form submit event This function needs to take 2 parameters : EVENT and ID My problem now is when I call this function i can't specify the event param without having error so my formdata and my id will be undefined in my logs. Is there any better way to handle my form ?

 updatePost(event, id) { 
  const postId = id;
  const updatedPost = new FormData(event.target); // Any way to not use event param to save this ? 
  console.log(updatedPost, postId);
},
 // Call in my Template but : 
 <form @submit.prevent="updatePost(post.id)">

ps : form composed by 2 inputs type text and 1 input type file.Vue.js screenShot


Solution

  • The original event is available as $event inside the v-on: handler.

    Docs: Methods in Inline Handlers

    Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special $event variable

    So this should work:

     <form @submit.prevent="updatePost($event, post.id)">