属性绑定,是单向绑定,通过data中的数值变量(存的是数据或者类名或者其他)来渲染页面,而页面上对数据的更改,并不会影响data中的数据。
属性绑定通常那个用来从服务器获取数据后的渲染页面。
而v-model则是双向绑定,主要用来表单的验证输入
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
[v-cloak]{
display:none
}
#app{
width: 20%;
margin: 200px auto;
line-height: 40px;
border: 1px solid #ccc;
padding: 10px;
}
.box1{
height: 50px;
width: 100;
background: red;
}
.box2{
height: 50px;
width: 100;
background: blue;
}
</style>
</head>
<body>
<div id="app">
<a v-bind:href="url">百度</a>
<a :href="url">百度</a>
<button @click="change">改变a标签的url</button>
<div :class="cla"></div>
<button @click="changeClass1">改变元素的背景色为蓝色</button>
<button @click="changeClass2">改变元素的背景色为红色</button>
</div>
<script type="text/javascript" src="./js/vue.min.js"></script>
<script type="text/javascript">
var vm=new Vue({
el: "#app",
data:{
url:'http://www.baidu.com',
cla:"box1"
},
methods:{
change:function(){
this.url='http://www.360.com'
},
changeClass1:function(){
this.cla='box2'
},
changeClass2:function(){
this.cla='box1'
}
}
})
</script>
</body>
</html>
实际上就是:
<div :class="cla"></div>
<button @click="changeClass1">改变元素的背景色为蓝色</button>
<button @click="changeClass2">改变元素的背景色为红色</button>
changeClass1:function(){
this.cla='box2'
},
changeClass2:function(){
this.cla='box1'
}
.box1{
height: 50px;
width: 100;
background: red;
}
.box2{
height: 50px;
width: 100;
background: blue;
}