重学vue笔记之动态样式(class与style)

说明

class与style使用起来有点相似,注意判别

使用class

使用class绑定的有3种格式:

  • 字符串
  • 数组
  • 对象
<template>
    <div :style="classString">
            字符串形式
    </div>
    <div :style="classObject">
            对象形式
    </div>
    <div :style="classArray">
            数组形式
    </div>
</template>
data() {
      return {
        classString: 'red',
        classObject: { red: false, green: true },
        classArray: ['red', 'green', {brown: false}]
      }
    },

使用style

使用style绑定的有2种格式:

  • 字符串
  • 对象
<template>
 <div :style="classString">
            字符串形式
    </div>
 <div :style="classObject">
            对象形式
 </div>
<template>
data() {
      return {
        styleString: 'background: orange',
        styleObject: {
          color: 'orange',
          background: 'yellow'
        }
      }
    },

子组件使用父组件定义的样式

父亲组件中的template部分

<template>
 <div>
      <child :class="styleString"/>
 </div>
<template>

父亲组件中的data部分

data() {
      return {
        styleString: 'background: orange'
      }
    },

child组件中的template部分

<template>
 <div>
      <div :class="$attrs.class">one</div>
      <div :class="$attrs.class">two</div>
 </div>
<template>

求点赞、关注

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值