v-bind动态绑定style

【对象语法】

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--对象语法-->
<div id="app">
<!-- <h2 :style="{key(属性名):value(属性值)}"></h2>-->
<h2 :style="{fontSize: size + 'px',backgroundColor: color}">{{message}}</h2>
<h2 :style="getStyle()">{{message}}</h2><!--调用函数-->
</div>

<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好!',
size: 60,
color: 'red'
},
methods: {
getStyle: function () {
return {fontSize: this.size + 'px',backgroundColor: this.color};
}
}
})
</script>
</body>
</html>

 

posted @ 2021-08-08 10:13  Mr_sven  阅读(69)  评论(0编辑  收藏  举报