百度一下 &_vue bind函数">
当前位置:   article > 正文

vue:v-bind动态绑定_vue bind函数

vue bind函数

一,v-bind介绍

在这里插入图片描述

二,v-bind实践

<body>
    <div id="app">
        <!-- 错误做法 -->
        <!-- <img src="{{imgURL}}" alt=""> -->
        <img v-bind:src="imgURL" alt="">
        <a v-bind:href="aHref">百度一下</a>
        <!-- 语法糖的写法 -->
        <img :src="imgURL" alt="">
        <a :href="aHref">百度一下</a>
    </div>

    <script src="./js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                imgURL: '此处为图片url',
                aHref: 'https://www.baidu.com/?tn=44048691_1_oem_dg'
            }

        })
    </script>


</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

三,v-bind动态绑定class

1.对象语法

在这里插入图片描述

css:

  .active {
            color: red;
        }
  • 1
  • 2
  • 3

html:

<body>
    <div id="app">
        <h2 :class="{active:isActive}">{{message}}</h2>
        <!-- 将class的类名封装成函数的写法 -->
        <h2 :class="getClass()">{{message}}</h2>
        <button v-on:click="btnClick">点击变色</button>
    </div>

    <script src="./js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                message: '哈喽',
                isActive: true
            },
            methods: {
                btnClick: function() {
                    this.isActive = !this.isActive
                },
                // 如果class较多,也可以封装成一个函数,如下
                getClass: function() {
                    return {
                        active: this.isActive
                    }
                }
            }

        })
    </script>


</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
2.数组语法

css:

  .active {
            color: red;
        }
  • 1
  • 2
  • 3

html:

<body>
    <div id="app">
        <h2 :class="[userClass]">{{message}}</h2>
        <!-- 将class的类名封装成函数的写法 -->
        <h2 :class="getClass()">{{message}}</h2>
    </div>

    <script src="./js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                message: '哈喽',
                userClass: 'active'
            },
            methods: {
                // 如果class较多,也可以封装成一个函数,如下
                getClass: function() {
                    return [this.userClass]
                }
            }

        })
    </script>


</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

四,v-bind动态绑定style

1.对象语法
<body>
    <div id="app">
        <!-- <h2 :style="{属性名:属性值}">{{message}}</h2> -->
        <!-- 属性值采用驼峰命名法 -->
        <h2 :style="{fontSize:finalSize+'px',backgroundColor:color}">{{message}}</h2>
         <!-- 属性值用基本格式,但需要加'' -->
        <h2 :style="{'font-size':finalSize+'px','background-color':color}">{{message}}</h2>
        <!-- 将style封装成函数的写法 -->
        <h2 :style="getStyle()">{{message}}</h2>

    </div>

    <script src="./js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                message: '哈喽',
                finalSize: 100,
                color: 'red',
            },
            methods: {
                // 如果style较多,也可以封装成一个函数,如下
                getStyle: function() {
                    return {
                        fontSize: this.finalSize + 'px',
                        backgroundColor: this.color,
                         // 属性值基本命名格式,加''
                        // 'font-size': this.finalSize + 'px',
                        // 'background-color': this.color
                    }
                }
            }

        })
    </script>


</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
2.数组语法
<body>
    <div id="app">
        <!-- 数组写法 -->
        <h2 :style="[fs,bgc]">{{message}}</h2>
        <!-- 将style封装成函数的写法 -->
        <h2 :style="getStyle()">{{message}}</h2>

    </div>

    <script src="./js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                message: '哈喽',
                fs: {
                    fontSize: '200px'
                },
                bgc: {
                    backgroundColor: 'red'
                },

            },
            methods: {
                // 如果style较多,也可以封装成一个函数,如下
                getStyle: function() {
                    return [this.fs, this.bgc]
                }
            }

        })
    </script>


</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/436147
推荐阅读
相关标签
  

闽ICP备14008679号