Currently I have something like the following code inside my <script>
:
render(createElement) {
return createElement("form",
{ref: "formEl" ,
on: {submit: this.handleSubmit}
},
[ <insert create form inputs here> ]);
}
handleSubmit(e) {
e.preventDefault();
//Other stuff that also rely on using e
}
If I place a submit button inside the form handleSubmit works and runs perfectly.
But if I were to do this.$refs.formEl.submit()
HandleSubmit does not run.
I have tried a solution posted in: VueJs 2 - preventDefault() on $refs.form.submit()
By adding a event listening like so:
this.$refs.form.addEventListener("submit", this.handleSubmit);
Sadly this did not work.
Edit (After Comments):
To elaborate on "not work":
what I found to happen is when pressing a submit button contained inside the <form>
tags HandleSubmit()
is actually ran twice (im assuming dude to adding a listener twice)
And I added some console.log()
to handlesubmit and found nothing on console.
The component is made in Typescript with classes and I am pretty certain it is in the equivalent to the methods section of the component for typescript:
(inside export default class ClassName extends Vue {}
)
What I find is that if I attach a button outside the <form>
tag that triggers the submit()
method on the reference the default action of updating the URL with the form contents is performed.
handleSubmit
function is actually inside themethods
section of the component? – Yom T.ref
attribute on the form component manually make any difference for you? Sounds like you wanted to programmatically invoke the form submission outside of the component. – Yom T.