Vue style 绑定样式
对象写法(推荐)、数组写法
对象写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定样式</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<!-- 绑定 style 样式 对象写法 -->
<div class="basic" :style="styleObj">你好</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#root',
data: {
styleObj: {
fontSize: '40px',
color: 'red',
}
},
})
</script>
<style>
.basic {
height: 100px;
width: 100px;
}
</style>
</html>
数组写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定样式</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<!-- 绑定 style 样式 数组写法 -->
<div class="basic" :style="[styleObj,styleObj2]">你好</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#root',
data: {
styleObj: {
fontSize: '40px',
color: 'red',
},
styleObj2: {
backgroundColor: 'blue'
}
},
})
</script>
<style>
.basic {
height: 100px;
width: 100px;
}
</style>
</html>

浙公网安备 33010602011771号