溫馨提示×

vue中動態添加style樣式的寫法有哪些

vue
小億
182
2024-03-08 15:02:24
欄目: 編程語言

  1. 使用對象語法:
<template>
  <div :style="dynamicStyles"></div>
</template>

<script>
export default {
  data() {
    return {
      dynamicStyles: {
        color: 'red',
        fontSize: '16px',
      }
    };
  }
};
</script>
  1. 使用數組語法:
<template>
  <div :style="[baseStyles, dynamicStyles]"></div>
</template>

<script>
export default {
  data() {
    return {
      baseStyles: {
        color: 'blue',
        fontSize: '14px',
      },
      dynamicStyles: {
        color: 'red',
        fontSize: '16px',
      }
    };
  }
};
</script>
  1. 使用計算屬性:
<template>
  <div :style="computedStyles"></div>
</template>

<script>
export default {
  data() {
    return {
      color: 'red',
      fontSize: '16px',
    };
  },
  computed: {
    computedStyles() {
      return {
        color: this.color,
        fontSize: this.fontSize,
      };
    }
  }
};
</script>

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女