• Vue动态绑定class


    目录

    绑定对象

    绑定数组

    用在组件上

    组件内只有一个根元素

    组件内有多个元素


    class与动态class是可以一起使用的

    绑定对象

    :class = "{ 类名1: 判断条件1, 类名2: 判断条件2, ... }"

    如果类名后面对应的条件成立,则类名就会添加

    案例

    1. <script setup lang="ts">
    2. import { ref } from 'vue'
    3. const flag = ref(true)
    4. script>
    5. <style>
    6. .box {
    7. width: 500px;
    8. height: 300px;
    9. margin: 50px auto;
    10. }
    11. .bg {
    12. background: pink;
    13. }
    14. style>

    当然,我们也可以直接给class绑定一个对象

    1. <script setup lang="ts">
    2. import { ref } from 'vue'
    3. const flag = ref(true)
    4. const styleObj = ref({ bg: flag.value })
    5. script>
    6. <style>
    7. .box {
    8. width: 500px;
    9. height: 300px;
    10. margin: 50px auto;
    11. }
    12. .bg {
    13. background: pink;
    14. }
    15. style>

    也可以绑定一个计算属性

    1. <script setup lang="ts">
    2. import { ref, computed } from 'vue'
    3. const flag = ref(true)
    4. const styleObj = computed(() => ({ bg: flag.value }))
    5. script>
    6. <style>
    7. .box {
    8. width: 500px;
    9. height: 300px;
    10. margin: 50px auto;
    11. }
    12. .bg {
    13. background: pink;
    14. }
    15. style>

    绑定数组

    也可以使用一个数组来绑定多个 class,数组中我们可以写  三元表达式  对象形式  直接写类名

    :class="[ '直接写类名' , { 类名1: 判断条件1, 类名2: 判断条件2, ... } , 类名 ?条件1:条件2 , ... ]"

    1. <script setup lang="ts">
    2. import { ref } from 'vue'
    3. const flag = ref(true)
    4. script>
    5. <style>
    6. .box {
    7. margin: 50px auto;
    8. }
    9. .bg {
    10. background: pink;
    11. }
    12. .w500 {
    13. width: 500px;
    14. }
    15. .h300 {
    16. height: 300px;
    17. }
    18. style>

    用在组件上

    组件内只有一个根元素

    会直接添加到根元素上

    父组件

    1. <script setup lang="ts">
    2. import { ref } from 'vue'
    3. import Son from './Son.vue'
    4. const flag = ref(true)
    5. script>
    6. <style>
    7. .box {
    8. margin: 50px auto;
    9. }
    10. .bg {
    11. background: pink;
    12. }
    13. .w500 {
    14. width: 500px;
    15. }
    16. .h300 {
    17. height: 300px;
    18. }
    19. style>

    子组件

    组件内有多个元素

    需要指定哪个根元素来接收这个 class。可以通过组件的 $attrs 属性来实现指定:

    $attrs会把props没有声明接收的,全部接收过来,起到了一个兜底的作用

    父组件

    1. <script setup lang="ts">
    2. import { ref } from 'vue'
    3. import Son from './AboutView.vue'
    4. const flag = ref(true)
    5. script>
    6. <style>
    7. .box {
    8. margin: 50px auto;
    9. }
    10. .bg {
    11. background: pink;
    12. }
    13. .w500 {
    14. width: 500px;
    15. }
    16. .h300 {
    17. height: 300px;
    18. }
    19. style>

    子组件

    1. <script setup lang="ts">
    2. import { useAttrs } from 'vue'
    3. // 可以使用 useAttrs 这个函数,查看没有被 props 接收的数据
    4. const attrs = useAttrs()
    5. console.log(attrs)
    6. script>

  • 相关阅读:
    《HTML+CSS+JavaScript》之第3章 基本标签
    Impala解决cast导致UDF ERROR: Decimal expression overflowed
    Linux命令200例:dip用于用户与远程主机建立通信连接
    php如何处理高并发请求
    js中的数据结构:栈,队列,链表,字典&哈希表,树
    编译安装redis及配置多实例
    2021 个人年度小结
    SQL存储过程详解
    Kotlin 变量详解:声明、赋值与最佳实践指南
    微信小程序实现微信支付的相关操作设置
  • 原文地址:https://blog.csdn.net/qq_52845451/article/details/133773258