Vue 3 and Bootstrap 5 seem to be struggling with finding the properties of an undefined 'backdrop'

I am facing an issue with Vue3 (Vite) and Bootstrap 5 while trying to create a modal that can be called by code. The problem arises when I attempt to open the modal from its parent component.

Bootstrap has been properly installed and included in my project:

import bootstrap (main.js)

Adding

import bootstrap/dist/js/bootstrap
does not resolve the error.

I have created a basic modal that listens for a prop to determine when to open it.

However, upon opening the modal, the following error is thrown:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'backdrop')

<template>
    <div class="modal" tabindex="-1" id="errModal" aria-labelledby="ErrorModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Error {{ this.occurred }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"
                        aria-label="ErrorModalLabel"></button>
                </div>
                <div class="modal-body">
                    <p>{{ this.error_message }}</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">OK</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { Modal } from "bootstrap"
export default {
    props: {
        occurred: String,
        error_message: String,
        show: Boolean,
    },
    components: {
        myModal: null
    },
    data() {
        return {
        }
    },
    methods: {
    },
    mounted() {
        this.myModal = new Modal(document.getElementById('errModal'))
    },
    watch: {
        show: function (newVal, oldVal) { // watching it
            this.cam_prop = newVal.properties
        },
    }
};
</script>

Calling the Modal from Parent Component:

<RegIdentErrorModalVue id="#ErrModal" :show="this.error" :occurred="'Identification'" :error_message="this.error_text">
        </RegIdentErrorModalVue>

Attempting to create the Modal using new bootstrap.Modal results in an error (bootstrap not defined).

I suspect the issue might be related to importing, as styling works but functionality is affected. Could it possibly be due to Vite?

Answer №1

It appears that the modal is being initialized before the DOM is fully loaded. One solution would be to initialize the component within the mounted lifecycle hook of the parent component. Here's an example:

   Parent Component
<template>
    ...
    <RegIdentErrorModalVue v-if="isMountedComponent" id="#ErrModal" :show="this.error" :occurred="'Identification'" :error_message="this.error_text" />
    ...
</template>

<script>
export default {
    data() {
        return {
            isMountedComponent: false,
        }
    },
    mounted() {
        this.isMountedComponent = true;
    },
}
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble arises when attempting to showcase document fields in MongoDB

As a beginner in programming, I am putting in my best effort to figure things out on my own. However, I seem to be stuck without any guidance. I am attempting to display all products from the mongoDB based on their brand. While I have successfully set up a ...

Tips for including a JSON file within the utils directory of a Node.js project

I have a JavaScript file located in the utils folder of my Node.js project. This JS file is responsible for retrieving data from a database. However, at the moment, I only have mock data stored in a local JSON file. Now, I need to figure out how to load th ...

Step-by-step guide for dynamically including dropdown options

Is there a way to dynamically add a dropdown using JavaScript? I currently have a dropdown with numbers that creates another dropdown when selected. However, I want to add the dynamic dropdown through JavaScript. How can I achieve this? Below is the PHP ...

Steps to Hide a Material-UI FilledInput

Trying to format a FilledInput Material-ui component to show currency using the following package: https://www.npmjs.com/package/react-currency-format Various attempts have been made, but none seem to be successful. A codesandbox showcasing the issue has ...

Toggle a jQuery bar based on the presence of a specific CSS class

I am working on a feature where I can select and deselect images by clicking on a delete button. When an image is selected, a bar appears at the top. If I click the same delete button again, the image should be deselected and the bar should disappear. This ...

Chrome geolocation feature does not display the permission popup for JavaScript

After the latest Chrome update, we have noticed that the popup to Allow/Block accessing a user's location from a website is no longer appearing. We tested this on Edge, Firefox, and different mobile devices, where it seems to be working fine. Current ...

AngularJS - Choose and establish initial values for Editing or Creating New Items

My first project involving AngularJS has left me a bit stuck when it comes to using the select list. I need to either set the default value to the first option for a new entry, or if it's an edit, select the appropriate value. In my form, there are t ...

Managing Pop-Ups when browsing on Internet Explorer

I've been working on an Excel VBA macro that automates several tasks on the Medicare website. The macro opens IE, logs in, compares claims, and alerts me to any differences found. However, I've encountered a problem during the log-in step, specif ...

Arranging Elements in a Vertical Stack Using Bootstrap

I'm currently working with bootstrap to arrange a series of cards side by side, but I'm facing an issue where the cards end up stacking on top of each other instead of aligning horizontally. <div class="container"> <div class="row"& ...

What could be causing the fourth tab to not show any data while the first three tabs are functioning as expected?

I've encountered an issue with my HTML tabs. While I can easily switch between the first three tabs and view their content, tab 4 doesn't seem to display its associated data. It's puzzling why tab 4 is not working when the others are functio ...

React input value doesn't get updated on onChange event causing the input

Currently, I'm working on a React application that requires a table with inputs in each row. To achieve this, I am utilizing Material-UI and Formik. However, I've encountered a bug where input fields lose focus whenever I type something into them ...

Is it more important to focus on passing props or making API requests the top priority in your web application structure

Context Currently, I am working on a Vue App utilizing the Composition API. My backend serves as the source of data for loading pages and components. Inquiry When it comes to structuring my app's architecture, should I prioritize passing props thro ...

Converting an HTML table to a CSV file with pure JavaScript

I need to implement a CSV download feature on my website that converts the HTML table into downloadable content. While searching for a suitable plugin, I came across resources like http://www.dev-skills.com/export-html-table-to-csv-file/ which uses PHP s ...

When there is a duplicate route in Vue-router and the back-end, which one will handle the request first - Vue.js or the back-end?

Picture this scenario: I have a domain named test.com serving a Single Page Application (SPA) with a route /home, and there is also a back-end serving the SPA which has a route called /home as well. Who will respond first if I type test.com/home in my br ...

Numerous status buttons with a variety of background colors

There are 3 buttons on the order page: [New Order], [Processing], and [Completed]. When the processing button is clicked, the background of the button should change and the order status will be updated in the database. The [New Order] button will have a ...

Ways to bypass a promise within a sequence

Currently, I am involved in a nodejs project and I am looking for a way to bypass a promise in a chain. Here is the code snippet I am working with. The initial promise block resolves a value of {success: true}. In the following block, I need to assess th ...

Get started with adding a Typescript callback function to the Facebook Login Button

I am in the process of implementing Facebook login into my Angular7 application using Typescript. Although I have successfully integrated Facebook's Login Button plugin for logging in, I am facing issues with providing a callback method to the button& ...

When an option that is already selected in AngularJS is clicked, the ng-model value will be updated

I currently have a select element with a data-ng-options attribute: <select data-ng-model='myValue'> <option value=''> Default </option> <option data-ng-repeat="i in item" value='{$ i.value $}'& ...

Steps for transforming Object A into an array of objects, each containing the properties and assigned values and labels from Object A

I have an object with specific key-value pairs and I would like to transform it into an array where each item has a label and a value: { "id": 12432, "application": "pashmodin", "unit": null, "status": ...

How can curly braces be utilized in an array reduce method?

If the function `fn3` is used instead of `fn2` in this code snippet running on node 9.5.0, the `console.log(totalUpvotes)` will display `undefined`. Shouldn't it actually be equal to 92? const posts = [{id: 1, upVotes: 2}, {id:2, upVotes: 89}, {id: ...