1、对style的绑定例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title-hellovuejs</title>
<style>
</style>
</head>
<body>
<div id ="app">
<!-- <h2 :style = "{key(属性名):value(属性值)}"> {{message}}</h2> -->
<h2 :style = "{fontSize:'50px'}"> {{message}}</h2>
<h2 :style = "{fontSize:sizevalue,backgroundColor:colorvalue}"> {{message}}</h2>
<h2 :style = "getStyle()"> {{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
//let(变量) /const(常亮)
const chen1 = new Vue({
el: '#app', //用于挂载要管理的元素
data:{//定义数据
message:'你好啊,helloword',
sizevalue: '100px',
colorvalue: 'red',
},
methods:{
getStyle:function(){
return {fontSize:this.sizevalue,color:this.colorvalue}
}
}
})
</script>
</body>
效果如下
2、数组形式对style的绑定例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title-hellovuejs</title>
<style>
</style>
</head>
<body>
<div id ="app">
<!-- <h2 :style = "{key(属性名):value(属性值)}"> {{message}}</h2> -->
<h2 :style = "{fontSize:'50px'}"> {{message}}</h2>
<h2 :style = "{fontSize:sizevalue,backgroundColor:colorvalue}"> {{message}}</h2>
<h2 :style = "getStyle()"> {{message}}</h2>
<!-- 数组形式对style的绑定例子 -->
<h2 :style = "[style1,style2]"> {{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
//let(变量) /const(常亮)
const chen1 = new Vue({
el: '#app', //用于挂载要管理的元素
data:{//定义数据
message:'你好啊,helloword',
sizevalue: '100px',
colorvalue: 'red',
style1:{backgroundColor:'blue'},
style2:{fontSize:'80px'}
},
methods:{
getStyle:function(){
return {fontSize:this.sizevalue,color:this.colorvalue}
}
}
})
</script>
</body>