SoFunction
Updated on 2025-04-04

Analysis of v-bind attributes, class and style usage of vue basics

This article describes the v-bind attribute, class and style usage of vue basics. Share it for your reference, as follows:

1. Attributes

property:

v-bind:src=""
width/height/title....

Abbreviation:

:src="" recommend

<img src="{{url}}" alt="">The effect can be produced, but there will be a 404 error
<img v-bind:src="url" alt="">The effect can be released, and no 404 request will be sent

=function(){
      new Vue({
        el:'#box',
        data:{
          url:'/img/bd_logo1.png',
          w:'200px',
          t:'This is a beautiful picture'
        },
        methods:{
        }
      });
    };
<div >
    <!--<img src="{{url}}" alt="">-->
    <img :src="url" alt="" :width="w" :title="t">
  </div>

2. class and style

:class=""     v-bind:class=""
:style=""     v-bind:style=""
:class="[red]"    red is data
:class="[red,b,c,d]"

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;
    .red{
      color: red;
    }
    .blue{
      background: blue;
    }
  &lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
          claOne:'red',//The red here is the class class name          claTwo:'blue'
        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;!--HerecalOne,calTworefer todataData in--&gt;
    &lt;strong :class="[claOne,claTwo]"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

:class="{red:true, blue:false}"//Here is { json}

&lt;style&gt;
    .red{
      color: red;
    }
    .blue{
      background: blue;
    }
  &lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;div &gt;
    &lt;strong :class="{red:true,blue:true}"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;
    .red{
      color: red;
    }
    .blue{
      background: blue;
    }
  &lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
          a:true,
          b:false
        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;strong :class="{red:a,blue:b}"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

data:{
json:{red:a, blue:false}
}

:class="json"

Official recommended usage

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;
    .red{
      color: red;
    }
    .blue{
      background: blue;
    }
  &lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
          json:{
            red:true,
            blue:true
          }
        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;strong :class="json"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

style:
:style="[c]"

.red{
      color: red;
    }
&lt;div &gt;
    &lt;strong :style="{color:'red'}"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;

:style="[c,d]"

Note: Compound style, using camel nomenclature

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;&lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
          c:{color:'red'},//The red here is class.red          b:{backgroundColor:'blue'}//Note: Compound style, using camel nomenclature        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;strong :style="[c,b]"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

:style="json"

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;&lt;/style&gt;
  &lt;script src=""&gt;&lt;/script&gt;
  &lt;script&gt;
    =function(){
      new Vue({
        el:'#box',
        data:{
          a:{
            color:'red',
            backgroundColor:'gray'
          }
        },
        methods:{
        }
      });
    };
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;strong :style="a"&gt;Word...&lt;/strong&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

I hope this article will be helpful to everyone's programming.