【问题标题】:How do access to vee-validate errors and clear them outside vue instance (component)如何访问 vee-validate 错误并在 vue 实例(组件)之外清除它们
【发布时间】:2018-10-23 07:04:42
【问题描述】:

我想在我关闭模式后或在我将数据发送到数据库并在 vue 实例之外执行此操作后清除错误,我尝试过 vm.$validator.errors.clean() 并重置,但这些对我不起作用,任何建议如何在外部访问它们组件?

 Vue.component('add-modal', {
    template: '#add-modal-template',
    mounted() {
        $(document).on('hidden.bs.modal','.modal', function (e) {
            $(this).remove('bs.modal');
            $(this).removeData('bs.modal');
            $(".modal-body input").val(" ");
            $('.modal-body option:first').prop('selected',true);
            console.log($(this).find('.modal-body').html());
            var myBackup = $(this).find('.modal-body').html();
            $('.modal-body').empty();
            $('.modal-body').append(myBackup);

            console.log(vm.$validator.Errors);
        });
    }
    }

    var vm = new Vue({
        el: '#app',
        data: {},
        created: function(){
            //if something is need to be loaded first
        }
    });

});


    <script type="text/x-template" id="add-modal-template">
    <!-- Modal -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLongTitle">Add new deal</h5>
                    <button type="button" class="close no-glow" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="addDealForm" @submit.prevent="" v-on:submit.prevent>
                        <div class="form-group">
                                <label>Deal title</label>
                                <input type="text" name="title" class="form-control" id="Contact Person" v-validate="'required|max:15'" placeholder="Enter deal title" v-model="deal.title">
                                <span class="error text-danger" v-show="errors.has('title')">{{errors.first('title')}}</span><br>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-primary no-glow" data-dismiss="modal">Close</button>
                            <button class="btn btn-success no-glow">Save</button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</script>

我提供了代码示例

【问题讨论】:

  • 你试过clear()方法吗?示例:jsfiddle.net/hcac5je0/1
  • 您建议的方法在 vue 实例内,但我需要从 vue 外部访问它。如果我 console.log vm.$validator.errors 它说未定义
  • 如何注册 Vue 组件并获取对 vm.$validator.errors 的引用?我需要查看更多代码来检测问题。
  • 我添加了一些代码,所以我需要在 jquery 函数中清除 vue 组件 add-modal 中的错误

标签: vue.js vuejs2 vee-validate


【解决方案1】:

您的回调中的this 不是 Vue 实例。您可以尝试以下技术,分配var self=this

 Vue.component('add-modal', {
  template: '#add-modal-template',
  mounted() {
   var self = this;
   $(document).on('hidden.bs.modal', '.modal', function(e) {
    $(this).remove('bs.modal');
    $(this).removeData('bs.modal');
    $(".modal-body input").val(" ");
    $('.modal-body option:first').prop('selected', true);
    console.log($(this).find('.modal-body').html());
    var myBackup = $(this).find('.modal-body').html();
    $('.modal-body').empty();
    $('.modal-body').append(myBackup);

    console.log(self.errors);
    self.errors.clear()
   });
  }
 }

【讨论】:

  • 几乎都为我工作,我为此做了额外的功能,但有什么建议如何让该代码工作?
  • 能否请您在这里查看与 VeeValidate 相关的问题:stackoverflow.com/questions/59488066/…?
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 2020-12-08
  • 2019-07-18
  • 2021-08-25
  • 2017-09-19
  • 2020-11-23
相关资源
最近更新 更多