单击按钮即可打开Vue.js模式 [英] Vue.js open modal by click of a button
问题描述
如何使用按钮显示其他组件中的模态?例如,我具有以下组件:
How to use a button to show the modal in other components? For example, I have the following components:
info.vue
<template>
<div class="container">
<button class="btn btn-info" @click="showModal">show modal</button>
<example-modal></example-modal>
</div>
</template>
<script>
import exampleModal from './exampleModal.vue'
export default {
methods: {
showModal () {
// how to show the modal
}
},
components:{
"example-modal":exampleModal
}
}
</script>
exampleModal.vue
exampleModal.vue
<template>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
hihi
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</template>
如何显示exampleModal.vue中的模态?我知道我可以 使用data-toggle和data-target来显示模式,如下所示:
How to show the modal from exampleModal.vue? I know that I can use data-toggle and data-target to show the modal, like this:
<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>
但是有什么方法可以通过使用"showModal"方法来显示模态吗?
But is there any way to show the modal by using the method "showModal"?
推荐答案
根据您的代码段,您使用的是经典的Bootstrap模式.在这种情况下,您只需要使用 data-toggle
和data-target
属性:
According to your snippet, you're using a classic Bootstrap modal. You just need to use data-toggle
and data-target
attributes in that case:
<div id="app">
<div class="container">
<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>
<example-modal></example-modal>
</div>
</div>
编辑
我看错了问题,所以我编辑了答案.
Edit
I misread the question so i edit my answer.
可以使用自定义方法打开模式.您只需要使用refs
查找元素"#exampleModal",然后使用引导程序方法(
It's possible to open the modal with a custom method. You just need to find the element "#exampleModal" by using refs
and then open the modal with a bootstrap method (Bootstrap Programmatic API)
<div id="app">
<div class="container">
<button class="btn btn-info" @click="showModal">show modal</button>
<example-modal ref="modal"></example-modal>
</div>
</div>
methods: {
showModal() {
let element = this.$refs.modal.$el
$(element).modal('show')
}
}
这篇关于单击按钮即可打开Vue.js模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!