javascript - 页面打开时表单自动提交

标签 javascript vue.js

我的登陆页面上有一个表单。并将参数发送到 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/

相关文章:

javascript - meteor 账户资料

javascript - VueJS 嵌套构建器示例

javascript - 主脚本中的 Vuejs 模板限制

javascript - 为什么表的 td node.appendChild 不起作用?

javascript - svg 在两点之间绘制虚线钟形曲线

自动计算字符串中连续字母的JavaScript函数

vue.js - vue-router:如何检查当前路线是路线还是其子路线之一?

javascript - 数组分配后Vue中的无限更新循环

javascript - VueJS 中的条件验证

javascript - jQuery 动态生成 grep()