我的登陆页面上有一个表单。并将参数发送到 vuex actions
。当我点击submit
按钮时它就起作用了,并按应有的方式将我推送到下一页。但有一个缺陷。当我打开页面或刷新页面时。它会自动提交参数,我不知道为什么?
模板
<form class="flexbox" @submit.prevent="submit" method="post">
<keyword-filter />
<div class="seperator"></div>
<city-filter />
<submit-btn />
</form>
脚本
created(){
this.$store.dispatch("bridalApi", {
input: this.formItems,
});
},
methods: {
submit(){
this.$store.dispatch("bridalApi", {
input:this.formItems
})
.then(this.$router.push('home'));
},
},
computed: {
formItems(){
let paramObj = {
keyword: this.$store.state.bridal.keyword,
city: this.$store.state.bridal.city,
price: this.$store.state.bridal.price,
people: this.$store.state.bridal.people,
rank: this.$store.state.bridal.rank,
sort: this.$store.state.bridal.sort,
}
return paramObj;
},
}
此外,当我刷新页面或进入页面时,它会发送参数(我不希望这样)。但它不会让我进入下一页?我不太确定为什么它要提交参数自动?
最佳答案
我认为问题出在这里
created(){
this.$store.dispatch("bridalApi", {
input: this.formItems,
});
},
created Hook 可用于在创建实例后运行代码,因此每当您重新加载页面时,都会创建组件并触发存储中的操作。 查看https://v2.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram了解更多信息。
希望有帮助!
关于javascript - 页面打开时表单自动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597181/