vue.js - 组件的 Vue 提交按钮不提交

标签 vue.js vue-component

我在 Vue 中有一个组件,我用它来代替提交按钮。我可以将一个处理程序传递给它,组件在禁用自身并将其状态设置为已加载后调用该处理程序,并且它可以在错误后恢复(再次启用自身)并在一切顺利时显示漂亮的成功动画。这很好用,代码如下:

// Submit.vue
<template>
    <button :type="type" :class="classes" :disabled="loading" @click="onClick">
        <span class="flag" v-if="flag"></span>
        <slot></slot>
    </button>
</template>

<script>
    import _ from 'lodash'
    export default {
        name: 'submit',
        props: {
            brand: {
                type: String,
                default: 'primary'
            },
            // If no handler is provided, will fallback to normal submit button
            handler: {
                type: Function,
                required: false
            },
            flag: {
                type: Boolean,
                default: false
            }
        },
        data () {
            return {
                loading: false,
                success: false
            }
        },
        computed: {
            type () {
                return typeof this.handler !== 'undefined' ? 'button' : 'submit'
            },
            classes () {
                return [
                    `btn btn-${this.brand}`,
                    this.loading && !this.success ? 'loading' : null,
                    this.success ? 'success' : null
                ]
            }
        },
        methods: {
            onClick (event) {
                if (this.success) {
                    event.preventDefault()
                    return
                }
                this.loading = true
                if (typeof this.handler !== 'undefined') {
                    // Handler must return a Promise
                    this.handler.call()
                        .then(_.bind(() => {
                            this.onSuccess()
                        }, this))
                        .catch(() => {})
                        .then(_.bind(() => {
                            this.loading = false
                        }, this))
                }
            },
            resetSuccess () {
                this.success = false
            },
            onSuccess () {
                this.success = true
                setTimeout(this.resetSuccess, 2000)
            }
        }
    }
</script>

如果没有传递任何处理程序,它会退回到普通的提交按钮,假设您想要的只是在提交表单时自动禁用按钮。唯一的问题是当我单击从组件创建的按钮时未提交表单

我认为使用 onClick 方法通过 JS 强制提交会相当容易,但我想了解为什么不这样做。是浏览器问题吗?安全问题? Vue 问题?或者我错过的其他东西可能就在我面前?

这是一个用于快速测试的 JSFiddle:https://jsfiddle.net/03fgwgy5/ 代码并不完全相同,因为我使用的是单文件组件,但要点是相同的,并且可以很容易地观察到行为。

最佳答案

在检查处理程序 是否存在之前,您将loading 设置为false。由于您将按钮的 disabled 属性绑定(bind)到 loading,因此您禁用了按钮并阻止触发 native 点击事件。

在检查处理程序后,只需设置 loading 属性即可:

if (typeof this.handler !== 'undefined') {
  this.loading = true;
  ...
}

Here's a fiddle.

关于vue.js - 组件的 Vue 提交按钮不提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594356/

相关文章:

vue.js - 在一个 Vue 2 全新项目中有这么多漏洞是正常的吗?

vue.js - 如何求和并将其显示在 vuetify 数据表中

vue.js - 是否可以在外部文件中提取 vuetify 规则以重用它?

javascript - 在处理加载数据事件后使用 v-for VueJS 填充选择选项

javascript - 无法将 Prop 传递给组件的数据

typescript - Vue.js 3和typescript:类型'ComponentPublicInstance上不存在 '$store'属性

css - 使用 Transition 在 Vuejs 中的 v-if 上设置动画高度

javascript - 如何将 JQuery 转换为 Vue.js 脚本

vue.js - Vue Axios 动态 URL

vue.js - 视觉 : What is the cleanest way to select a component via CSS?