一、动态绑定变量
1. 直接将连接地址写到src中,会将其当做字符串解析
2. 前面加v-bind:会将其当做变量解析,再去对应的Vue模块加载
: v-bind的语法糖
<body>
<div id="first">
<img src="imageUrl" alt="404 picture">
<img src="https://www.baidu.com/img/hqydong_4f3f63f09807e2a2535ee5c2b6100511.gif" alt="404 picture">
<img v-bind:src="imageUrl" alt="404 picture"><br>
<a href="websiteURL"> 百度一下</a>
<a href="https://www.baidu.com/">百度一下</a>
<a :href="websiteURL">百度一下</a>
</div>
<script>
let one = new Vue({
el: "#first", data: {
name: "shuzhan",
imageUrl: "https://www.baidu.com/img/hqydong_4f3f63f09807e2a2535ee5c2b6100511.gif",
websiteURL: 'https://www.baidu.com/',
}
});
</script>
</body>
二. 动态绑定class属性
2.1. 基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind绑定class</title>
<style>
.first {
color: red;
}
.second {
background: black;
}
.third {
height: 200px;
}
</style>
</head>
<body>
<div id="module">
<h2 class="first second third">{{message}}</h2>
<h2 v-bind:class="firstStyle">{{message}}</h2>
<h2 class="second third" v-bind:class="firstStyle">{{message}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
let app = new Vue({
el: '#module',
data: ({
message: '舒展',
firstStyle: 'first',
secondStyle: 'second',
thirdStyle: 'third',
})
})
</script>
</body>
</html>
2.2. 对象语法
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.first {
color: red;
}
.second {
background: black;
}
.third {
height: 200px;
}
</style>
</head>
<body>
<div id="module">
<h2 v-bind:class="{first:true,second:true,third:true}">{{message}}</h2>
<h2 v-bind:class="{first:isFirstStyle,second:isSecondStyle,third:isThirdStyle}">{{message}}</h2>
<button v-on:click="changeColor()">切换颜色按钮</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
let app = new Vue({
el: '#module',
data: ({
message: '舒展',
isFirstStyle: true,
isSecondStyle: true,
isThirdStyle: true,
}),
methods: {
changeColor: function () {
this.isFirstStyle = !this.isFirstStyle;
}
}
})
</script>
</body>
</html>
三、动态绑定style属性
3.1. 对象语法
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="module">
<h2 style="color: red; background: blue; font-size: 100px">{{message}}</h2>
<h2 v-bind:style="{color:'red',background:'black',fontSize:'100px'}">{{message}}</h2>
<h2 v-bind:style="{color:finalColor,background:finalBgcolor,fontSize:finalSize}">{{message}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
let app = new Vue({
el: '#module',
data: ({
message: '舒展',
finalColor: 'red',
finalBgcolor: 'black',
finalSize: '100px'
})
})
</script>
</body>
</html>