1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 3.Vue动态绑定属性

3.Vue动态绑定属性

时间:2019-02-17 21:07:49

相关推荐

3.Vue动态绑定属性

1. v-bind的基本使用

某些时候我们并不想将变量放在标签内容中,像这样<h2>{{message}}</h2>是将变量h2标签括起来,类似js的innerHTML。但是我们期望将变量imgURL写在如下位置,想这样<img src="imgURL" alt="">导入图片是希望动态获取图片的链接,此时的imgURL并非变量而是字符串imgURL,如果要将其生效为变量,需要使用到一个标签v-bind:,像这样<img v-bind:src="imgURL" alt="">,而且这里也不能使用Mustache语法,类似<img v-bind:src="{{imgURL}}" alt="">,这也是错误的。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>v-bind的基本使用</title></head><body><div id="app"><!-- 错误的做法这里不能使用Mustache语法 --><!-- <img v-bind:src="{{imgURL}}" alt=""> --><!-- 正确的做法使用v-bind指令 --><img v-bind:src="imgURL" alt=""><a v-bind:href="aHerf"></a><!-- 语法糖写法 --><img :src="imgURL" alt=""><a :href="aHerf"></a></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{message:"你好啊",imgURL:"/th?id=OIP.NaSKiHPRcquisK2EehUI3gHaE8&pid=Api&rs=1",aHerf:""}})</script></body></html>

<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><style>.active {color: #FF0000;}</style></head><body><div id="app"><!-- <ul><li v-for="(item, index) in movies" :key="index" :class="{active : index === currentIndex}" @click="change(index)">{{index+"---"+item}}</li></ul> --><ul><li v-for="(item, index) in movies" :key="index" :class="index === currentIndex ? 'active' : ''" @click="change(index)">{{index+"---"+item}}</li></ul></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{currentIndex:0,movies:["海王","海贼王","火影忍者","复仇者联盟"]},methods:{change(index){this.currentIndex = index}}})</script></body></html>

此时vue对象中定义的imgURL变量和aHerf变量可以动态的绑定到img标签的src属性和a标签的href属性。v-bind:由于用的很多,vue对他有一个语法糖的优化写法也就是:,此时修改imgURL变量图片会重新加载。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>v-bind动态绑定class(对象语法)</title><style>.active{color:red;}</style></head><body><div id="app"><!-- <h2 class="active">{{message}}</h2><h2 :class="active">{{message}}</h2> --><!-- 动态绑定class对象用法 --><!-- <h2 :class="{key1:value1,key2:value2}">{{message}}</h2><h2 :class="{类名1:true,类名2:boolean}">{{message}}</h2> --><h2 class="title" :class="{active:isActive}">{{message}}</h2><h2 class="title" :class="getClasses()">{{message}}</h2><button @click="change">点击变色</button></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{message:"你好啊",active:"active",isActive:true},methods: {change(){this.isActive = !this.isActive},getClasses(){return {active:this.isActive}}},})</script></body></html>

定义两个变量activeisActive,在Dom元素中使用:class={active:isActive},此时绑定的class='active',isActive为true,active显示,定义方法change()绑定在按钮上,点击按钮this.isActive = !this.isActive,控制Dom元素是否有class='active'的属性。

2.2. v-bind动态绑定class(数组用法)

class属性中可以放数组,会依次解析成对应的class。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>v-bind动态绑定class(数组用法)</title><style></style></head><body><div id="app"><!-- 加上单引号当成字符串 --><h2 class="title" :class="['active','line']">{{message}}</h2><!-- 不加会被当成变量 --><h2 class="title" :class="[active,line]">{{message}}</h2><h2 class="title" :class="getClasses()">{{message}}</h2></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{message:"你好啊",active:"aaaa",line:'bbbb'},methods: {getClasses(){return [this.active,this.line]}},})</script></body></html>

加上单引号的表示字符串

不加的会当成变量

可以直接使用方法返回数组对象

3. v-for和v-bind结合

使用v-for和v-bind实现一个小demo,将电影列表展示,并点击某一个电影列表时候,将此电影列表变成红色。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>作业(v-for和v-bind的结合)</title><style>.active{color:red;}</style></head><body><div id="app"><ul><li v-for="(item, index) in movies" :key="index" :class="{active:index===currentIndex}" @click="changeColor(index)" >{{index+"---"+item}}</li></ul></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{currentIndex:0,movies:["海王","海贼王","火影忍者","复仇者联盟"]},methods: {changeColor(index){this.currentIndex = index}},})</script></body></html>

v-for时候的index索引,给每行绑定事件点击事件,点击当行是获取此行索引index并赋值给currentIndex,使用v-bind:绑定class,当index===currentIndexDom元素有active的class,颜色变红。

4. v-bind动态绑定style

4.1 v-bind动态绑定style(对象语法)

<!-- <h2 :style="{key(属性名):value(属性值)}">{{message}}</h2> --><!-- 加单引号,当成字符串解析 --><h2 :style="{fontSize:'50px'}">{{message}}</h2><!-- 不加单引号,变量解析 --><h2 :style="{fontSize:fontSize}">{{message}}</h2><h2 :style="getStyle()">{{message}}</h2>

4.2 v-bind动态绑定style(数组语法)

<div id="app"><h2 :style="[baseStyle]">{{message}}</h2><h2 :style="getStyle()">{{message}}</h2></div><script src="/npm/vue@2.6.10/dist/vue.js"></script><script>const app = new Vue({el:"#app",data:{message:"你好啊",baseStyle:{backgroundColor:'red'}},methods: {getStyle(){return [this.baseStyle]}},})</script>

类似绑定class,绑定style也是一样的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。