bind的使用
- v-bind使用场景是在我们要动态给元素属性赋值时,就需要用到v-bind;mustache语法时不能解决这个问题的
- v-bind语法糖写成冒号就好了
- v-bind在操作选择器的时候还可以传对象、数组;并且不会覆盖之前的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.Active {
color:red;
}
</style>
</head>
<body>
<div id="app">
<!-- mustache语法只能在元素内容里面使用-->
<!-- <img src="{{imgUrl}}" alt="">-->
<!-- 当我们要给元素中的属性动态赋值时,请使用bind;它会自动解析出来你想要的-->
<img v-bind:src="imgUrl" alt="">
<!-- 语法糖写法-->
<img :src="imgUrl" alt="">
<!-- 动态给属性绑定对象-->
<!-- <h1 class="title" :class="{Active:isActive, Line:inLine}">中国人</h1>-->
<!-- <button v-on:click="btnClick">点击</button>-->
<!-- 给选择器传数组-->
<h1 class="title" :class="['Active', 'Line']">中国人</h1>
<h1 class="title" :class="getClasses()">中国人</h1>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue ({
el:'#app',
data:{
imgUrl:'../img/test/生命地球.jpg',
isActive:true,
inLine:true,
thisActive:'Active',
thisLine:'Line'
},
methods:{
getClasses:function (){
return [this.thisActive,this.thisLine];
},
btnClick:function () {
this.isActive = !this.isActive;
}
}
})
</script>
</body>
</html>