11

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

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

<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"?

2
  • but which component do you want to display on click ? userInfoModal or exampleModal ? Commented May 19, 2018 at 18:12
  • I want to show exampleModal on click. thanks, I have edited the question Commented May 20, 2018 at 1:10

2 Answers 2

12

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.

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')
  }
}

Fiddle example

Sign up to request clarification or add additional context in comments.

1 Comment

This is not working in Nuxtjs. I have created my BootstrapVue modal within my components directory. I have added it to my pages now when I try to restart my server I get the error '$' is not defined
7

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

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

2 Comments

This is the best answer
how would you do this if I'm not using BootstrapVue, just standard bootstrap4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.