[英]VueJS PreventDefault form action on submit not working with ref?
Currently I have something like the following code inside my <script>
:目前,我的<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.如果我在表单中放置一个提交按钮,handleSubmit 可以正常运行。 But if I were to do this.$refs.formEl.submit()
HandleSubmit does not run.但是如果我这样做this.$refs.formEl.submit()
HandleSubmit 不会运行。
I have tried a solution posted in: VueJs 2 - preventDefault() on $refs.form.submit()我已经尝试过发布在: VueJs 2 - preventDefault() on $refs.form.submit()中的解决方案
By adding a event listening like so: this.$refs.form.addEventListener("submit", this.handleSubmit);
通过像这样添加一个事件监听: 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.编辑(评论后) :详细说明“不起作用”:我发现发生的是当按下<form>
标签中包含的提交按钮时, HandleSubmit()
实际上运行了两次(我假设老兄添加了两次监听器)和我添加了一些console.log()
来处理提交,但在控制台上什么也没找到。
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 {}
)该组件是在 Typescript 中制作的,带有类,我很确定它相当于 typescript 组件的方法部分:(内部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.我发现,如果我在<form>
标记之外附加一个按钮,该按钮会触发引用上的submit()
方法,则会执行使用表单内容更新 URL 的默认操作。
This seems to be working fine (without a ref
).这似乎工作正常(没有ref
)。 No doubled submission issue for me.对我来说没有双重提交问题。
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 {}
)该组件是在 Typescript 中制作的,带有类,我很确定它相当于 typescript 组件的方法部分:(内部export default class ClassName extends Vue {}
But here's a quick demo in Javascript.但这里有一个 Javascript 中的快速演示。 (The Typescript equivalent should be close enough). (等效的 Typescript 应该足够接近)。
new Vue({ el: '#app', components: { TheForm: { props: { prevent: Boolean }, render(createElement) { return createElement("form", { //ref: "formEl", on: { submit: this.handleSubmit } }, this.$slots.default); }, methods: { handleSubmit(e) { if (this.prevent) { e.preventDefault(); } //Other stuff that also rely on using e } } } } })
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script> <div id="app"> <p>Clicking on this should do nothing, since the default page-redirecting behavior is prevented.</p> <the-form action="/should-be-prevented" prevent> <input type="submit" value='Do "prevented" submission' /> </the-form> <p>This should work like normal submit buttons would. And once submitted, the whole screen should go blank (showing that the page has been redirected somewhere else).</p> <the-form action="/should-redirect"> <input type="submit" value='Do "normal" submission' /> </the-form> </div>
Form submission with ref
:使用ref
提交表单:
new Vue({ el: '#app', components: { TheForm: { props: { prevent: Boolean }, render(createElement) { return createElement('form', { on: { submit: this.handleSubmit } }, this.$slots.default); }, methods: { handleSubmit(e) { if (this.prevent) { e.preventDefault(); } else { this.$el.submit(e); } //Other stuff that also rely on using e } } } } })
form { border: 2px dotted lightgray; margin: 1rem auto; padding: 1rem; }
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script> <div id="app"> <the-form ref="form1" action="/should-be-prevented" prevent> This form should stay, since the default page-redirecting behavior is prevented. </the-form> <the-form ref="form2" action="/should-redirect"> This should work like normal forms would. And once submitted, the whole screen should go blank (showing that the page has been redirected somewhere else). </the-form> <hr /> <p>Note that these buttons are <strong>outside</strong> of the respective forms, to demonstrate programmatic form-submission trigger. </p> <input @click="$refs.form1.handleSubmit($event)" value='Do "prevented" submission' type="button" /> <input @click="$refs.form2.handleSubmit($event)" value='Do "normal" submission' type="button" /> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.