01-v-bind基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<img v-bind:src="imageUrl" alt="zz">
<a :href="baiduUrl">百度一下</a>
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
imageUrl: 'https://img14.360buyimg.com/n0/jfs/t1/117516/19/11234/321017/5ef9c08dE19765e41/f12e794d4ecf208b.jpg'
,baiduUrl: 'http://www.baidu.com'
}
})
</script>
</body>
</html>
02-v-bind动态绑定class属性(对象语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.active{
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 class="bingo" :class="{active: this.isActive, line:this.isLine}">{{message}}</h2>
<h2 class="bingo" :class="getClass()">{{message}}</h2>
<button @click="changeColor">变色</button>
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
isActive: true,
isLine: true
},
methods: {
changeColor: function (){
this.isActive = !this.isActive;
},
getClass: function (){
return {active: this.isActive, line:this.isLine}
}
}
})
</script>
</body>
</html>
03-v-bind动态绑定class属性(数组语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.active{
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 :class="[isActive, isLine]">{{message}}</h2><!--数组语法是写死的-->
<h2 :class="getClass()">{{message}}</h2><!--数组语法是写死的-->
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
isActive: 'aaa',
isLine: 'bbb'
},
methods: {
getClass: function (){
return [this.isActive, this.isLine]
}
}
})
</script>
</body>
</html>
04-v-bind和v-for结合作业
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.active{
color: red
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(b,index) in movies" :class="getClass()" @click="changeColor">{{b}}</li>
</ul>
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
movies: ['大话西游','功夫','回魂夜','少林足球','喜剧之王','007'],
active: false
},
methods: {
changeColor: function (event){
alert('我变成红色啦')
},
getClass: function (){
return {active: this.active}
}
}
})
</script>
</body>
</html>
05-v-bind动态绑定style(对象语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2 :style="getStyles()">{{message}}</h2>
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
fontSize: 100,
fontColor: 'red'
},
methods: {
getStyles: function (){
return {fontSize: this.fontSize + 'px', color: this.fontColor}
}
}
})
</script>
</body>
</html>
06-v-bind动态绑定style(数组语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2 :style="[baseStyle, upperStyle]">{{message}}</h2>
</div>
<script src="../../js/vue.js"></script>
<script>
let app = new Vue({
el: '#app',
data: {
message: 'hello bingo',
baseStyle: {backgroundColor : 'red'},
upperStyle: {fontSize: '200px'}
}
})
</script>
</body>
</html>