Araf Nishan
Araf Nishan

Reputation: 53

How to trigger Bootstrap-vue modal on page load without button or link?

Sorry for the help request, but I can't work out how to get a bootstrap-vue modal to display initially on page load without needing to trigger it with a button or link.

Thanks.

Upvotes: 4

Views: 6376

Answers (1)

blaz
blaz

Reputation: 4068

Instead of trying to open modal on page load, you can open it on mounted event of Vue.

Elevated from sample code on Bootstrap-Vue website:

<template>
  <div>
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
      <div class="d-block text-center">
        <h3>Hello From My Modal!</h3>
      </div>
      <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
      <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
    </b-modal>
  </div>
</template>

<script>
  export default {
    methods: {
      showModal() {
        this.$refs['my-modal'].show()
      },
      hideModal() {
        this.$refs['my-modal'].hide()
      }
    },
    mounted() {
      this.showModal();
    }
  }
</script>

Upvotes: 9

Related Questions