Javascript Vue.js 通过单击按钮打开模态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50428046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:36:28  来源:igfitidea点击:

Vue.js open modal by click of a button

javascriptvue.jsvuejs2bootstrap-4

提问by user3717119

How to use a button to show the modal in other components? For example, I have the following components:

如何使用按钮在其他组件中显示模态?例如,我有以下组件:

info.vue

信息.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

示例Modal.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">&times;</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>

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:

如何从exampleModal.vue 显示模态?我知道我可以使用 data-toggle 和 data-target 来显示模态,如下所示:

<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>

<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>

But is there any way to show the modal by using the method "showModal"?

但是有没有办法使用“showModal”方法来显示模态?

回答by Sovalina

According to your snippet, you're using a classic Bootstrap modal. You just need to use data-toggleand data-targetattributes in that case:

根据您的代码段,您使用的是经典的 Bootstrap 模式。在这种情况下,您只需要使用data-toggledata-target属性:

<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.

我误读了这个问题,所以我编辑了我的答案。

It's possible to open the modal with a custom method. You just need to find the element "#exampleModal" by using refsand then open the modal with a bootstrap method (Bootstrap Programmatic API)

可以使用自定义方法打开模态。您只需要通过使用找到元素“#exampleModal” refs,然后使用引导方法(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')
  }
}

Fiddle example

小提琴示例

回答by Lucas Santos

Without jQuery you could create a function on "method:" block like this:

如果没有 jQuery,您可以在“method:”块上创建一个函数,如下所示:

openEditModal(data){
  // <handle with your data>
  this.$root.$emit("bv::show::modal", "your-modal-id");
}