学习指令的作用主要是将值插入到我们的模板的内容当中。
v-bind主要用于实习动态绑定:
- 作用:动态绑定属性
- 缩写:::
- 预期:any (with argument) | Object (without argument)
v-bind的使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--错误做法:这里不可以使用mustache语法 -->
<!-- <img src="{{imgURL}}" alt="">-->
<!--正确的做法:使用v-bind指令 (动态绑定属性)-->
<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>
const app = new Vue({
el:'#app',
data:{
message:'你好啊',
imgURL:'http://inews.gtimg.com/newsapp_ls/0/11148715054_294195/0.jpg',
aHref:'http://www.baidu.com'
}
})
</script>
</body>
</html>
v-bind绑定class
绑定class有两种方式:
- 对象语法:class后面跟的是一个对象。
- 数组语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
.active{
color: red;
}
</style>
<body>
<div id="app">
<!-- <h2 class="active">{{message}}</h2>-->
<!-- <h2 :class="active">{{message}}</h2>-->
<!-- <h2 v-bind:class="{key1: value1,ke2: value2}">{{message}}</h2>-->
<!-- <h2 v-bind:class="{类名1: boolean,类名2: boolean}">{{message}}</h2>-->
<h2 class="title" v-bind:class="{active: Isactive,line: Isline}">{{message}}</h2>
<h2 class="title" v-bind:class="getClasses()">{{message}}</h2>
<button v-on:click="btnclick">按钮</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
message:'你好啊',
Isactive:true,
Isline:true
},
methods: {
btnclick: function () {
this.Isactive = !this.Isactive
},
getClasses:function () {
return {active: this.Isactive,line: this.Isline}
}
}
})
</script>
</body>
</html>
v-bind绑定class的数组语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!-- <h2 v-bind:class=""> {{message}}</h2>-->
<!-- 加上单引号表示一个字符串,不加表示一个常量-->
<!-- <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="../js/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
message:'你好啊',
active:'jaijia',
line:'youyou'
},
methods:{
getClasses:function () {
return [this.active,this.line]
}
}
})
</script>
</body>
</html>
练习
点击列表中的任何一项,该列表的文字变成红色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
.active{
color: red;
}
</style>
<body>
<!--作业:点击列表中的任何一项,该列表的文字变成红色-->
<div id="app">
<!-- item 表示一个变量,可以改变-->
<ur>
<!-- <li v-for="item in movice">{{item}}</li>-->
<li v-for="(m,index) in movice":class="Isactive(index)" v-on:click="changeactive(index)">{{m}}</li>
<!-- <li class="[active:m==index]" v-for="(m,index) in movice">{{index}}-{{m}}</li>-->
</ur>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
movice:['狐妖小红娘','犬夜叉','海外'],
active:[{'active': true},{'active': false},{'active': false}]
},
methods:{
Isactive:function (index) {
return this.active[index]
},
changeactive:function (index) {
for (let i=0;i<this.active.length;i++){
this.active[i].active=(index==i?true:false)
}
}
}
})
</script>
</body>
</html>
来源:CSDN
作者:just-so-so.
链接:https://blog.csdn.net/weixin_43911280/article/details/104263300