VueJS ---- v-bind

Posted by legio on Fri, 31 Jan 2020 19:47:49 +0100

v-bind

v-bind deals with tag attributes in html. For example, < div > < div > is a tag, < img > is also a tag,
For example, bind an src to an Img tag
html:

<div id="app">
    <img v-bind:src="imgSrc"  width="200px">
</div>

js:
Add imgSrc attribute in data for html to call

var app=new Vue({
    el:'#app',
    data:{
          imgSrc:'http://baidu.com/wp-content/uploads/2017/02/vue01-2.jpg'
     }
})

v-bind abbreviation

<!-- Complete grammar -->
<a v-bind:href="url"></a>
<!-- Abbreviation -->
<a :href="url"></a>

Binding css Style

In our work, we often use v-bind to bind css styles:
When Binding css style, the bound value must be declared in the data attribute in vue.

1. Directly bind class style

<div < :class="className">1,binding classA</div>

2. Bind class A and make a judgment. It will be displayed when isOk is true

< div <: class = "{classA: isok}" > 2. Judgment in binding class < / div >

3. Bind array in class

< div: class = "[classA, ClassB]" > 3. Bind arrays in class < / div >

4. Use ternary operator to judge in binding class

< div: class = "isok? ClassA: ClassB" > 4. Judgment of ternary operator in binding class < / div >

5. Binding style

<div :style="{color:red,fontSize:font}">5,binding style</div>

6. Binding Style with object

<div :style="styleObject">6,Bind with object style style</div>
var app=new Vue({
   el:'#app',
   data:{
       styleObject:{
           fontSize:'24px',
           color:'green'
            }
        }
    })

Complete code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-bind</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
    <style>
        .classA{
            color:red;
        }
        .classB{
            font-size: 150%;
        }
    </style>
</head>
<body>
    <h1>v-bind</h1>
    <hr>
    <div id="app">
        <p><img v-bind:src="imgSrc" width="200px;" height="200px;" alt="sss"></p>
        <p><a :href="baidu" target="_blank">Baidu</a></p>
        <hr>
        <div :class="className">1.binding class</div>
        <div :class="{classA:isOk}">2.binding class Judgement in</div>
        <div :class="{classA,classB}">3.binding class Array in</div>
        <div :class="isOK?classA:classB">4.binding class Ternary operators in</div>

        <hr>
        <div>
            <input type="checkbox" id="isOK" v-model="isOk">
            <label for="isOk">isOk={{isOk}}</label>
        </div>

        <hr>
        <div :style="{color:red,fontSize:font}">
            5.binding Style    
        </div>
        <div :style="{styleObject}">
            5.binding Style object
        </div>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el:'#app',
            data:{
                message:'hello world',
                imgSrc: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=415293130,2419074865&fm=27&gp=0.jpg',
                baidu:'http://www.baidu.com',
                className: 'classA',
                isOk:false,
                classA:'classA',
                classB:'classB',
                red:'blue',
                font:'20px',
                styleObject:{
                    color:'green',
                    fontSize:'20px'
                }

            }
        });
    </script>
</body>
</html>

Topics: Vue Attribute Javascript