RGB案例
使用v-model + v-bind 属性绑定 + 模板字符串实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/vue-2.6.12.js"></script>
<style>
.box {
width: 200px;
height: 200px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="app">
<div>
<span>R:</span>
<input type="text" v-model.number="r">
</div>
<div>
<span>G:</span>
<input type="text" v-model.number="g">
</div>
<div>
<span>B:</span>
<input type="text" v-model.number="b">
</div>
<hr>
<!-- 专门用户呈现颜色的 div 盒子 -->
<!-- 动态绑定了一个样式对象 -->
<div class="box" :style="{ backgroundColor: `rgb(${r},${g},${b})` }">
<!-- 模板字符串 -->
{{`rgb(${r},${g},${b})`}}
</div>
<button @click="show">按钮</button>
</div>
<script>
// 创建 Vue 实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
// 红色
r: 0,
// 绿色
g: 0,
// 蓝色
b: 0
},
});
</script>
</body>
</html>
计算属性可以做到什么呢
//所有的计算属性要定义在 computed节点下
computed: {
//计算属性定义成方法格式
rgb(){
// 模板字符串
return `rgb(${this.r},${this.g},${this.b})`
}
}
使用该属性
{{ rgb }}
<div class="box" :style="{ backgroundColor: rgb }">