Handle Bootstrap modal hide event in Vue JS

后端 未结 5 1005
再見小時候
再見小時候 2020-12-16 12:49

Is there a decent way in Vue (2) to handle a Bootstrap (3) modal hide-event?

I found this as a JQuery way but I can\'t figure out how to capture this event in Vue:

5条回答
  •  臣服心动
    2020-12-16 13:17

    This may be late but another way if you are using a custom modal component (Modal.vue) you have created is to

    1. create a method in mounted to catch the event of closure (doesn't have to be the same name as below)
        mounted: function(){
            this.triggerHidden();
        }
    
    1. create the method
        methods: {
           triggerHidden: function(){
               var self = this;
               if( $('#getModal').length ){
                   $('#getModal').on('hidden.bs.modal', function(){
                      //catch the native bootstrap close event and trigger yours
                      self.#emit('modal-close');
                   });
               }
            }
        }
    
    1. now call use your custom event with your custom/reusable modal component

      The method doSomething will be called when the modal closes. You can also use the approach to hijack the other jquery event so its a little more manageable.

提交回复
热议问题