After closing the bootstrap vue alert, it refuses to function properly

I've been experimenting with creating a global alert component using bootstrap vue alert. I'm utilizing the vuex store to manage the alert's state.

Below, you can find my Alert.vue component:

<template>
  <b-alert show :variant="variant" dismissible> {{ message }} </b-alert>
</template>
<script>
export default {
  props: {
    variant: String,
    message: String
  },
  data() {
    return {};
  },
  name: "Alert",

  methods: {},
  computed: {}
};
</script>
<style scoped></style>

Here is my vuex store:

const alert = {
  namespaced: true,
  state: {
    variant: "",
    message: "",
    showAlert: false
  },
  getters: {
    variant: state => state.variant,
    message: state => state.message,
    showAlert: state => state.showAlert
  },
  mutations: {
    setSuccessvariant(state) {
      state.variant = "success";
    },
    setDangervariant(state) {
      state.variant = "danger";
    },
    setMessage(state, message) {
      state.message = message;
    },
    showAlert(state) {
      state.showAlert = true;
    },
    hideAlert(state) {
      state.showAlert = false;
    }
  },
  actions: {}
};

export default alert;

I'm using the alert component in another component like this:

<alert v-if="showAlert" :message="message" :variant="variant"></alert>

The computation for showAlert in this component looks like this:

showAlert() {
      return this.$store.getters["alert/showAlert"];
   }

This function only appears to work the first time. The alert pops up when triggered initially, but once dismissed, it does not reappear upon subsequent triggers.

Answer №1

Modify the show attribute to use v-model. This way, when you click on the dismiss button, the value of showAlert will be updated in the Vuex store:

<b-alert 
  v-model="showAlert" 
  :variant="variant" 
  dismissible
> {{ message }} 
</b-alert>

...

computed: {
  showAlert() {
    get() {
      this.$store.getters["alert/showAlert"]
    },
    set(value) {
      // UPDATE THIS MUTATION
      this.$store.commit("setShowAlert", value)
    }
  }
}

Vuex:

mutations: {
  setShowAlert(state, value) {
    state.showAlert = value
  }, 
}

Additionally, consider changing v-if="showAlert" with v-show="showAlert" to see if there are any re-rendering issues with the component.

Answer №2

To ensure proper functionality on dismissed events, it is important to control the behavior of the component. The show attribute within the component does not accept values other than true or false, which may not always align with how you want the alert to be triggered.

It is crucial NOT to bind a v-model to a string attribute that you anticipate will never be null when the alert needs to be displayed. This approach will not yield the desired results.

This particular component lacks the capability to interpret non-boolean flags.

Instead, utilize v-bind:show as illustrated below:

<b-alert v-bind:show="error!=null" 
dismissible variant="danger" @dismissed="error=null">
        {{error}}
    </b-alert>

Upon dismissing the alert, the error variable will be set to null and the status of the show condition will toggle accordingly. Subsequently, if the error variable is assigned a value again, the state will update appropriately.

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

Adjust the width of a div based on a dynamic value in VUE 3

I need assistance with setting the width of my div based on a computed value. Scenario: I have products that retrieve information from an API, and the length value varies depending on the selected product. Therefore, this component needs to be dynamic. T ...

Ways to extract the content from the textarea

Currently, I am working on a project that involves using CKEditor. Within the project, there is a panel with various properties used to create a tooltip. I decided to utilize CKEditor to insert content into the tooltip because it provides an excellent user ...

Incorporate JavaScript code into contentWindow

Attempting to utilize a Javascript postMessage function, similar to how it can be done with an iframe, but now with an embed element. The reason for using an embed is due to the bug in IOS devices which causes issues with setting iframe width and height. ...

Learn how to personalize the global navigation bar in Vue specifically for each view

Just diving into the world of Vue and I'm loving it so far. I've been working on a Vue-CLI app with a navbar and content, where the navbar is consistent across all pages. However, I want to spice things up by adding some unique content to the nav ...

After a single click, the functionality of jquery.nav.js seems to be malfunctioning

Encountering an error message: Uncaught TypeError: Cannot read property 'top' of undefined(…) jquery.nav.js:183 In an effort to convert my web app into a Single Page Application (SPA) using the jquery.nav.js library (available at https://githu ...

React Component Functions for Export and Import

Currently working on a webapp built with React. My main component is defined in App.js, while I have another subcomponent responsible for creating buttons, like the logout button generated by renderLogoutButton(). But now, I want to reuse this function in ...

Which method is more effective: utilizing AJAX to retrieve page elements, or just toggling their visibility?

When it comes to web development, particularly in jQuery, should I preload my page and use jQuery to manipulate the DOM, or should I do it the other way around? This debate involves: <div id="item1" ></div> <div id="item2"></div> ...

The jquery UI button is displaying too wide even though the padding is set to zero

My goal is to decrease the width of certain jQuery buttons. Though I have attempted to eliminate padding using the code below, there remains a minimum of 5 pixels on either side of the text within my button. .button().css({ 'padding-left':' ...

Command in Selenium Webdriver for initiating a mouse click

Currently, I am in the process of writing tests for a Java application that has been created with the Vaadin framework. To conduct these tests, I have opted to utilize the Robot Framework. In certain instances, I must implement robot framework commands suc ...

Encountering an issue while attempting to fetch a value from a nested object property with VueJS Interpolation

I'm struggling to properly display nested arrays in Vue JS. Below is a snippet of my JSON data: { "id": 1, "slug": "test-page", "banner": { "title": "banner title", "subTitle": "my sub title", "hasSubTitle": false, "hasClass": " ...

vuejs function for modifying an existing note

Method for editing an existing note with Vue.js in a web application This particular application enables users to perform the following tasks: Create a new note View a list of all created notes Edit an existing note Delete an existing note Prog ...

Is there a way to transfer parameters from a Vue function to a component?

I am struggling to find the right solution for passing parameters to a function. I need to use NavigateTo to send a String value to my index in order to switch components without using Vue Router. Home.js Vue.component('Home', { props: [&apo ...

Guide on creating a JSONP request

My goal is to perform cross-site scripting. The code snippet below shows the jsonp method, which appears to fail initially but succeeds when switched to a get request. I am trying to achieve a successful response using the jsonp method. I have confirmed th ...

The argument for the e2e test is invalid because it must be a string value

Currently, I am conducting an end-to-end test for an Angular application using Protractor. However, I encountered an error while running the test for a specific feature file. The UI value that I am checking is - WORKSCOPES (2239) Below is the code snippe ...

Having trouble with CSS paths in EJS Node.JS Express when dealing with URLs containing multiple parameters?

Apologies for my limited English skills! Currently, I am working on a web application using NodeJS/Express and EJS as the template engine. I have encountered a problem in my development process. Below is the folder structure of my project: App folder/ ...

Middleware in Express not producing results

Here is the code I am currently using: var express = require('express'); var app = express(); app.use(express.bodyParser()); app.use(express.cookieParser()); var port = Number(process.env.PORT || 5000); app.get('/test/:id', function(r ...

Rails 7 Importmap Pins does not work with Jquery source

Query Why does the importmaps command-generated source not function correctly with Bootstrap 4.6.1, while a tweaked source for the same version of jquery does? Challenge Details I developed a rails 7 application using importmaps for handling the javascri ...

Converting a child.jsp file into a modal or overlay using JavaScript: A step-by-step guide

Is it possible to call a child.jsp as a model using JavaScript? I previously accessed the child jsp using showmodaldialog and window.open() with JavaScript, but it opens in another window. I want the child jsp to appear in a modal or overlay view. Can some ...

Fade in text and then vanish after a few moments

I am looking to implement a fading in and out effect for text that stops after a certain period of time. The goal is for the text to indicate that a user is online when they join, and then disappear after some time. Currently, the function does not have a ...

Creating dynamic buttons and textboxes on a JSP page

<form:form action="approveAccount.html" method="GET"> <input type="submit" value="approvAll"/> <p>List of Pending Accounts for Approval</p> <c:forEach items="${accountIdList}" var="val"> <li>${val}</li> ...