Handle Bootstrap modal hide event in Vue JS

后端 未结 5 995
再見小時候
再見小時候 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

      <custom-modal @modal-close="doSomething"></custom-modal> 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.

    0 讨论(0)
  • 2020-12-16 13:20

    Please see https://bootstrap-vue.js.org/docs/components/modal#overview There you can find event "hide" or "hidden" So you can bind this event:

    <b-modal ref="someModal" @hide="doSometing">
    
    0 讨论(0)
  • 2020-12-16 13:30

    Bootstrap uses JQuery to trigger the custom event hidden.bs.modal so it is not easily caught by Vue (which I believe uses native events under the hood).

    Since you have to have JQuery on a the page to use Bootstrap's native modal, just use JQuery to catch it. Assuming you add a ref="vuemodal" to your Bootstrap modal you can do something like this.

    new Vue({
      el:"#app",
      data:{
      },
      methods:{
        doSomethingOnHidden(){
          //do something
        }
      },
      mounted(){
        $(this.$refs.vuemodal).on("hidden.bs.modal", this.doSomethingOnHidden)
      }
    })
    

    Working example.

    0 讨论(0)
  • 2020-12-16 13:32

    If working with bootstrap-vue then below code snippet will be helpful:

    export default {
      mounted() {
        this.$root.$on('bv::modal::hide', (bvEvent, modalId) => {
          console.log('Modal is about to be shown', bvEvent, modalId)
        })
      }
    }
    

    for other events please refer to the official docs.

    0 讨论(0)
  • 2020-12-16 13:35

    One option is to tie it to a variable:

    data: function(){
      return {
           showModal: false
            //starts as false.  Set as true when modal opens. Set as false on close, which triggers the watch function.
    },
    watch: {
      showModal: function(){
        if(this.showModal == false){
         // do something
      },
    }
    

    HTML

    <button id="show-modal" @click="showModal = true">Show Modal</button>
    
     //later if using a component
    <modal v-if="showModal" @close="showModal = false">
    
     // or alternatively in the bootstrap structure
    <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal" @click="showModal = false">Close</button>
    </div>
    
    0 讨论(0)
提交回复
热议问题