v-bind 基础用法
<div id='app'>
<img src="{{imgURL}}">
<img src="imgURL">
<img v-bind:src="imgURL">
<img :src="imgURL">
<a :href="aHref">百度</a>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
imgURL: 'https://i0.hdslb.com/bfs/archive/e62b6b095ef38dfb742687f11e4b570dde420b5d.png',
aHref: 'http://www.baidu.com'
}
})
</script>
v-bind 动态绑定class(对象语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vuejs</title>
<script src="js/vue.js"></script>
<style>
.active{
color: red;
}
.font{
font-size: 50px;
}
</style>
</head>
<body>
<div id='app'>
<h2 class="active">{{message}}</h2>
<h2 :class="getClass()">{{message}}</h2>
<button v-on:click = "btnClick">按钮</button>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
isActive: 'true',
isFont: 'true'
},
methods: {
btnClick: function(){
this.isActive = !this.isActive
},
getClass: function(){
return {active: this.isActive, font: this.isFont};
}
}
})
</script>
</body>
</html>
v-bind 动态绑定class(数组语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vuejs</title>
<script src="js/vue.js"></script>
<style>
.active{
color: red;
}
.font{
font-size: 50px;
}
</style>
</head>
<body>
<div id='app'>
<h2 class="active">{{message}}</h2>
<h2 :class="getClass()">{{message}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
active: 'aaaa',
font: 'bbbb'
},
methods: {
getClass: function(){
return [this.active, this.font];
}
}
})
</script>
</body>
</html>
v-bind 动态绑定style(对象语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vuejs</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id='app'>
<h2 :style="{fontSize: '50px', color: 'red'}">{{message}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello'
}
})
</script>
</body>
</html>
v-bind 动态绑定style(数组语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vuejs</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id='app'>
<h2 :style="[basesStyle1, basesStyle2]">{{message}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
basesStyle1: {fontSize: '50px'},
basesStyle2: {color: 'red'}
}
})
</script>
</body>
</html>