Vue学习------------(4)v-bind
v-bind作用:将属性的值绑定到data的变量上、methods的方法中、computed的计算属性里
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<img v-bind:src="imgURL"> //v-bind语法糖
<a v-bind:href="aHref">百度一下</a> // :href="aHref" 等同于 v-bind:href="aHref"
</div>
<script src="../js/vue.js"></script>
<script>
const app=new Vue({
el : '#app',
data : {
imgURL : 'http://www.jxnu.edu.cn/_upload/site/00/05/5/logo.png' ,
aHref : 'http://www.baidu.com'
}
})
</script>
</body>
</html>
v-bind动态绑定class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.act {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 :class="{act: isAct , line: isLine}">{{message}}</h2>
<button v-on:click="btnClick">按钮</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app=new Vue({
el : '#app',
data : {
message: '你好啊' ,
isAct: true ,
isLine: true
},
methods: {
btnClick: function(){
this.isAct = !this.isAct
}
}
})
</script>
</body>
</html>
如果太复杂,可以放入methods或者computed中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.act {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 :class="{act: isAct , line: isLine}">{{message}}</h2>
<h2 :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: '你好啊' ,
isAct: true ,
isLine: true
},
methods: {
btnClick: function(){
this.isAct = !this.isAct
},
getClasses: function(){
return {act: this.isAct , line: this.isLine}
}
}
})
</script>
</body>
</html>
v-bind动态绑定style
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2 :style="{fontSize: finalSize , color: finalColor }">{{message}}</h2> //驼峰写法
<h2 :style="{'font-size': finalSize , color: finalColor }">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app=new Vue({
el : '#app',
data : {
message: '你好啊' ,
finalSize: '100px' ,
finalColor: 'red'
}
})
</script>
</body>
</html>
浙公网安备 33010602011771号