动态添加类 动态添加样式 ref获取dom

<template>
  <div class="home">
                                  <!-- 类名  判断是否添加的条件 -->
    <!-- <div class="div" :class="{'color':flag}"></div> -->
    <!-- 三目运算 -->
    <!-- <div class="div" :class="flag?'color':''"></div> -->

                    <!-- 数组: data中的属性  -->
    <div :class="[ifwidth,ifcolor]"></div>

  </div>
</template>

<script>
export default {
  name: 'Home',
  components: {
    // HelloWorld
  },
  data() {
    return {
      // 对象添加类名
      flag:true,
      // 数组添加类名
      // 属性:类名
      ifwidth:'div1',
      ifcolor:'color1'
    }
  }
}
</script>
<style lang="scss">
  .div {
    width: 100px;
    height: 100px;
    background: red;
  }
  // 对象
  .color {
    width: 200px;
    height: 200px;
    background-color: skyblue;
  }
// 数组
  .div1 {
    width: 100px;
    height: 100px;
  }
  .color1 {
    background-color: rgb(50, 39, 39);
  }
</style>

添加类名

<template>
  <div class="about">
    <h1 :style="{'color':'red','font-weight':200}">这是一个H1</h1>
 
    <h1 :style="styleObj1">这是一个H1</h1>
 
    <h1 :style="[styleObj1,styleObj2]">这是一个H1</h1>
  </div>
</template>
<script>
export default {
  data(){
    return {
      styleObj1:{color:'blue','font-weight':200,'font-size':'40px'},
      styleObj2:{'font-style':'italic'},
    }
  } 
}
</script>
<style lang="">
  
</style>

 vue2中获取dom元素

 <div ref="my"></div>

  <!-- this.$ref.my   (ref中的起的名字)-->

vue3中获取dom元素

<template>
  <a href="https://www.baidu.com/" ref="A">123</a>
</template>

<script>
import { useRouter } from 'vue-router'
import { ref, reactive, toRefs, onMounted } from 'vue'
export default {
  name: 'App',
  components: {
  },
  setup() {
    let A = ref(null)
    onMounted(() => {
      console.dir(A.value);
    })
    return {
     // 获取dom 后一定要暴露出去 否则就会返回 ref(null)  中定义的类型
      A
    }
  }
}
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值