在 Vue JS 中处理 Bootstrap 模态隐藏事件 [英] Handle Bootstrap modal hide event in Vue JS
问题描述
Vue (2) 中是否有处理 Bootstrap (3) 模态隐藏事件的好方法?
Is there a decent way in Vue (2) to handle a Bootstrap (3) modal hide-event?
我发现这是一种 JQuery 方式,但我不知道如何在 Vue 中捕获此事件:
I found this as a JQuery way but I can't figure out how to capture this event in Vue:
$('#myModal').on('hidden.bs.modal', function () {
// do something…
})
添加类似 v-on:hide.bs.modal="alert('hide')
之类的东西似乎不起作用.
Adding something like v-on:hide.bs.modal="alert('hide')
doesn't seem to work.
推荐答案
Bootstrap 使用 JQuery 来触发自定义事件 hidden.bs.modal
所以它不容易被 Vue 捕获(我相信它使用引擎盖下的本地事件).
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).
由于页面上必须有 JQuery 才能使用 Bootstrap 的本机模式,因此只需使用 JQuery 来捕获它.假设您将 ref="vuemodal"
添加到您的 Bootstrap 模态,您可以执行这样的操作.
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)
}
})
工作示例.
这篇关于在 Vue JS 中处理 Bootstrap 模态隐藏事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!