当元素的属性值要求为变量时,我们可以使用v-bind来修饰属性
v-bind的基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动态绑定</title>
<style>
.pclass1{
color: rebeccapurple;
background-color: red;
}
.pclass2{
color: rgb(6, 182, 226);
}
.pclass3{
font-size: 30px;
}
</style>
</head>
<body>
<div id="app">
<img v-bind:src="imgurl" alt="图片">
<!-- 语法糖写法 -->
<img :src="imgurl" alt="图片">
<!-- <p :class="{类名1: boolean, 类名2: boolean}">{{message}}</p> -->
<!-- 类名所在这个位置不会当做变量,但boolean所在的这个位置不加引号会当做变量,但是true和false还有数字没加引号也可以识别,加了引号才会当做字符串类型。 -->
<!-- 这里两个class不会谁优先级高就覆盖另外一个,vue会把他们两个合并起来 -->
<p class="pclass3" :class="{pclass1: active1, pclass2: active2}">{{message}}</p>
<!-- 还可以用这个替代上面那条语句 -->
<p class="pclass3" :class="getclasses()">{{message}}</p>
<!-- 用数组的形式代替 -->
<p class="pclass3" :class="[val1, val2]">{{message}}</p>
<!-- 这里两个class会根据谁优先级高就覆盖另外一个-->
<p :class="val" :class="{pclass1: active1, pclass2: active2}">{{message}}</p>
<button v-on:click="btnclick">按钮</button>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "hello",
imgurl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUEAAACdCAMAAAAdWzrjAAAA3lBMVEUAwYAAwH9J0ZEBv38zR18AwH0Awn0AyYEuYGczRF5I0pE/zY4Av3sySF9K0JIAvnz8+/0Au30Au3QAuXnu8/dczZ191bBJ2JM0P14yOl00R1wAuX41Ql48lnlQy5gAunTf7u7K7d87uYcuVWQtYGMjiHIvW2UzhG85cW1DxYknwYUtZ2Y4jHYXpHkxUWM1Olo7pn4ej3QqeGo8fHMRsHoYnHVHv4g7hXYrcGg+n382y4dGtYg7fnE0N108i3mY28Cg4cfT7eaF1bi35tNq0Kb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gVsQ37LeKxbzhK3uk5EdPWWOHP7UX5QyQfLhsN6guXaYg01j9SOZfwu9aXZLg1T+IGG6XKZ5tyb0ShoMJ91lKmoMmmyIOZJONgDGU++oo4JQdI8FDRfxfB7fZEHJJrHsxvPO3XG8FGhwk+T2NnTCrkzJRx2lUv7qhd14miRpRw2RDjuKE3E/T+QDqfpiMhRYwDuKZcy9vtEMJnliDpcdNRJKgz8l82Xo0FBsTSamW2IKGsZHnognTDPIOtMkDBMSZzlFxmB4PxeJWG03ocACshsnk1g4wODda8n8IQYTSm7ju25YtZhlt0DZWeNsLAQGF8tuVyZf9bwy6BCRduedtNqg4UaYhHo0TjvLPAaZysXzRMRTIYQXZjfZUAYJoSxM7m5DFFhGeHcbqzszkXjiUZLM75Nk/ndCHeJl2UMZZCqJiOVxUmHQvo9/AVg3ztZlvG/xJEnuY8bCSbyQmaR7k6S3UzGRBu+OEpmQ7yti0HQLjEO/Z0W1qqM78TyVQsLJcfxP3FnKMXIhVAx2484/8V2bYQ8uhaUrCzL40iSbDYvEkb+GTKRZL6YsSSc0QYEV0NWqjBzN1N3+aH6XZ0/dr45RL8vF6oYm8hkU+Awokwkk+60sAFFgPcSZ36zWXjEJPwex/D5/+J5zDMHnwOh0vYmHAp+FvviPAl8K0IcCnwOG34tBgy8GDb6UzB4KRBAEQRAEQRAEQRAEQRAEQRAEQf67/Auxf6s4PK8r4AAAAABJRU5ErkJggg==",
active1: true,
active2: false,
val: "pclass3",
val1: "pclass1",
val2: "pclass2"
// val1: "pclass1" val2: "pclass2"这个放在:class="{val1: active1, val2: active2}" 无效,类名所在这个位置不会当做变量,但boolean所在的这个位置不加引号会当做变量,加了引号才会当做字符串类型。
},
methods: {
btnclick: function(){
this.active1 = !this.active1
this.active2 = !this.active2
},
getclasses: function(){
return {pclass1: this.active1, pclass2: this.active2}
}
},
})
</script>
</body>
</html>
v-bind动态绑定style(对象语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- font_size 'font-size' font-size -->
<h2 :style="{fontSize: '50px'}">{{message}}</h2>
<h2 :style="{fontSize: finalsize1}">{{message}}</h2>
<h2 :style="{fontSize: finalsize2 + 'px'}">{{message}}</h2>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "sb",
finalsize1: "50px",
finalsize2: 50
}
})
</script>
</body>
</html>
v-bind动态绑定style(数组语法)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p :style="[csschange1,csschange2]">{{message}}</p>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "sb",
csschange1: {color: "red"},
csschange2: {fontSize: "100px"}
}
})
</script>
</body>
</html>