Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js bootstrap-vue dynamically change alert variant

So I'm using bootstrap-vue in a vue.js project, and this is the way you can display an alert acording to the docs:

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

What I'm trying to do is

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

//...

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

But this is not working, the alert is showing up withouth style. Is there any way I can use variables in the variant property of the alert, so I can dynamically change it from code??

like image 724
Jack Casas Avatar asked Aug 23 '18 14:08

Jack Casas


2 Answers

You need to bind the alertvariant prop to variant attribute like this:

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

Note :variant is a shortcut for v-bind:variant

More info on data binding here

like image 112
Allkin Avatar answered Sep 21 '22 23:09

Allkin


You can do it this way:

<b-button :variant="foo" @click="dataName = !dataName">Button Name</b-button>

//...

<script>
export default {
    name: 'componentName',
    data() {
        return {                       
            dataName: true //<--- define data
        }
    },
computed: {
    foo() {
      return this.dataName ? "success" : "warning"; //<--- define condition/s
    }
}
...
like image 21
Ismael Soschinski Avatar answered Sep 21 '22 23:09

Ismael Soschinski