首页 > 其他分享 >vuejs从入门到精通——Vue语法——Class 与 Style 绑定(绑定内联样式)

vuejs从入门到精通——Vue语法——Class 与 Style 绑定(绑定内联样式)

时间:2023-01-28 23:35:07浏览次数:32  
标签:Style Vue const vuejs 绑定 style 样式 template

Class 与 Style 绑定(绑定内联样式)

一、绑定对象

:style支持绑定 JavaScript 对象值,对应的是HTML 元素的style属性:

template:

const activeColor = ref('red')
const fontSize = ref(30)

template:

<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

尽管推荐使用 camelCase,但:style也支持 kebab-cased 形式的 CSS 属性 key (对应其 CSS 中的实际名称),例如:

template:

<div :style="{ 'font-size': fontSize + 'px' }"></div>
直接绑定一个样式对象通常是一个好主意,这样可以使模板更加简洁:
js:
const styleObject = reactive({
  color: 'red',
  fontSize: '13px'
})

template:

<div :style="styleObject"></div>
  同样的,如果样式对象需要更复杂的逻辑,也可以使用返回样式对象的计算属性。

二、绑定数组

     

标签:Style,Vue,const,vuejs,绑定,style,样式,template
From: https://www.cnblogs.com/zuoyang/p/17071528.html

相关文章