Changing alert variant dynamically with Vue.js and Bootstrap-Vue

As I work on a vue.js project using bootstrap-vue, I came across the following method to display an alert based on the documentation:

<b-alert variant="success" show>Success Alert</b-alert>

However, my attempt to achieve this is as follows:

<b-alert variant=alertvariant show>Success Alert</b-alert>

//...

<script>
export default {
    name: 'SetPOS',
    data() {
        return {                       
            alertvariant: "warning"
        }
    },
...

Despite this approach, the alert fails to display with the intended style. Is there a way for me to utilize variables in the variant property of the alert so that I can dynamically modify it through code?

Answer №1

To properly connect the alertvariant prop to the variant attribute, use this syntax:

<b-alert :variant="alertvariant" show>Success Alert</b-alert>

Keep in mind that :variant is a convenient shorthand for v-bind:variant

For more information on data binding, click here

Answer №2

Here is a way you can achieve it:

<b-button :variant="bar" @click="dataValue = !dataValue">Click Me</b-button>

//...

<script>
export default {
    name: 'myComponent',
    data() {
        return {                       
            dataValue: true //<--- setting initial value
        }
    },
computed: {
    bar() {
      return this.dataValue ? "primary" : "danger"; //<--- determining variant based on condition
    }
}
...

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

storing audio files locally with Vue.js

Looking for a way to store a sound locally for my Battleship game rather than referencing it on the internet. Here's the code that triggers the sound: @click.prevent="fireSound('http://soundbible.com/grab.php?id=1794&type=mp3')" I atte ...

Creating consistent image sizes in Bootstrap

I'm currently using Bootstrap 4 to showcase multiple images on my website. I have attempted to apply the Grid system as per Bootstrap's guidelines, but the display of the images is not aesthetically pleasing due to their uneven sizes, which can b ...

Disable the outer div scrolling in VueJS, but re-enable it once the inner div has reached the bottom of

I am currently working on a webpage that contains multiple divs stacked vertically. Here is the concept I am working with: Once the scrollbar reaches the bottom of the first div, the outer scrollbar will be disabled and the inner scrollbar will be enabled ...

Utilizing default values with props in Vue.js for customization

There are very few instances where I need to modify the value of props during initialization, for example: props : { columnName : String, setValue: { validator: function (value) { //handle specific cases let _value ...

There is an error message in Vue nextTick: "TypeError: this.method is not a function." The error only appears in my console, but the functionality seems to be working correctly in the browser

I'm experiencing an issue where I encounter an error when accessing a certain component for the second time in my browser. The strange thing is that everything appears to be working fine initially, but the error occurs when I try to perform actions li ...

Unable to display Apexcharts bar chart in Vue.js application

Struggling to incorporate Apexcharts into my Vue.js site, but unfortunately, the chart isn't appearing as expected. -For more guidance on Apexcharts, check out their documentation Here HTML <div class="section sec2"> <div id="chart" ...

"Enhance Your Vue Experience with CKEditor's External Link

I am currently engaged in a Vue project that employs ckeditor within one of its components. My goal is to configure hyperlinks so that they open in a new tab whenever an external link is detected. Based on the documentation, I understand that achieving th ...

The table is not visible when using Bootstrap Vue

Can anyone help me with displaying a table of flowers? I am having trouble making it show up on my page. Not quite sure how to use my data to get the flowers to display properly. Any guidance or advice would be greatly appreciated. <b-table> ...

Difficulty clearing dictionary in Vue.js causing issues with the DOM not updating

Check out this jsfiddle example I am working with a dictionary in Vue and using a v-for loop to render its items. I want to clear the dictionary, but simply setting it to an empty object or deleting keys doesn't trigger a re-render in the DOM. Any su ...

Guide on verifying the presence of a value in a textbox using vue

I'm currently working on a form that requires only numbers and text input. Any characters like ., ,, or spaces are not allowed in the textbox. Here are some of the attempts I've made, but unfortunately, they did not yield the desired results: i ...

Interconnected properties with varying behaviors in Vue versions 1 and 2

Encountering a problem with input boxes having formatted numbers and computed properties when using Vue 2. Whenever an input is changed, Vue automatically recomputes the number, making it difficult to type in exactly what is needed. new Vue({ el: ' ...

The functionality of the Vueify modal seems to be malfunctioning when incorporating Vueify alongside a central Modal.vue file that houses modal templates

I've been experimenting with integrating this tutorial on creating reusable modal dialogs with Vuejs and adapting it for use with Vueify. Initially, I was able to successfully implement it, but after exploring a different approach briefly, I returned ...

Stop Toast from closing when Modal is closed in BootstrapVue

Is there a way to prevent the Toast from closing when the Modal is closed in BootstrapVue? Here's the scenario: Open both the Modal and Toast on the page Close the Modal Notice that both the Modal and Toast close simultaneously Question: How can ...

Creating a dynamic webpage using the MVC design pattern within a Docker container

I am facing a challenge with my dotnet core application that has authorization. Due to company policy restrictions, Bearer tokens cannot be stored on the front-end. To work around this, I want to utilize an MVC Controller. However, the application is built ...

Laravel VueJS Vuetable-2 without user authentication

Initially, I attempted all the solutions provided here and on other websites. What I have experimented with: Vuetable-2 not working with Laravel Passport Unable to retrieve data from using vuetable-2, in Vuejs 2 The Issue: I was working on a project (L ...

How can Vue components be created without using a template?

How can I correctly implement the functionality needed for my application? After user authorization, it should return back to the application via a link with a callback function - localhost / callback. At this point, there is no need to render anything, as ...

Update the primary folder for the assets in the Vue build

Vue CLI 3.3 is what I'm using to develop projects for my vertical website with Vue. However, every time I build the project, the assets in dist/index.html always load from the root path. For example: <script src=js/chunk-vendors.b0f460c7.js>< ...

Utilizing Vue.js to fetch information from a post API in Node.js

I'm currently working on developing a post API in Node.js, with Vue.js as my frontend framework and using vue-resource to interact with APIs. Here is the submit function I have implemented on the frontend: validateBeforeSubmit() { var self = this ...

Is it possible to integrate external search functionality with Vuetify's data table component

I am currently working with the v-data-table component from Vuetify, which comes with a default filter bar in its properties. This table retrieves data from a local JSON file. The issue arises when I have another external component, a search bar created u ...

Guide to Utilizing the Import Function in a Vue 3 Template

Working on a Vue 3 project, my setup includes a stuff.ts file with helpful functions that I need to utilize in my template. <script lang="ts"> import { defineComponent, onMounted } from 'vue' import { doSomething } from ' ...