v-bind动态绑定style(对象语法、数组语法)

1、对象语法

在这里插入图片描述

1.1、方法一:当作属性值使用

<script>
	<div id="app">
	  //<h2 :style="{key(属性名):value:(属性值)}">{
    
    {message}}</h2>
	  //把50px当作属性值使用。如果50px不加单引号,那么vue会把50px当作成一个"变量"去data中找该变量,所以此处必须加上单引号
	  <h2 :style="{fontSize:'50px'}">{
    
    {
    
    message}}</h2>
	</div>
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
    
    
        el: '#app',
        data: {
    
    
          message:'你好呀'
        }
    })
</script>

在这里插入图片描述

1.2、方法二:当作变量使用

<div id="app">
  <!--把finalSize当作一个变量使用-->
  <h2 :style="{fontSize: finalSize + 'px' , backgroundColor: finalColor}">{
   
   {message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
      
      
        el: '#app',
        data: {
      
      
          message:'你好呀',
          finalSize: 100,
          finalColor: 'red',
        }
    })
</script>
</body>

在这里插入图片描述

1.3、方法三:(属性:值)来自与函数

<body>
<div id="app">
  <h2 :style="getStyles()">{
   
   {message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
      
      
        el: '#app',
        data: {
      
      
          message:'你好呀',
          finalSize: 100,
          finalColor: 'red',
        },
      methods: {
      
      
          getStyles: function(){
      
      
            return {
      
      fontSize: this.finalSize + "px", backgroundColor: this.finalColor}
          }
      }
    })
</script>
</body>

在这里插入图片描述

2、数组语法

在这里插入图片描述

<body>
<div id="app">
  <h2 :style="[baseStyle,baseStyle1]">{
   
   {message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
      
      
        el: '#app',
        data: {
      
      
          message:'你好呀',
          baseStyle: {
      
      backgroundColor: 'red'},
          baseStyle1: {
      
      fontSize: '100px'}
        }
    })
</script>
</body>

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_46112274/article/details/121110922