当前位置: 首页 > news >正文

:class的用法及应用

参考小满视频

在同一个标签中,class只能有一个,:class也只能有一个

:class的用法

1. :class = “非响应式的变量”(一般不使用,和写死了一样)

const a = "style1"
<span :class="a"></span>

浏览器中:
在这里插入图片描述

2. :class = “响应式对象”

const a = ref("style2")
<span :class="a"></span>

浏览器源码:
在这里插入图片描述

3. :class = “{对象形式}”

  • 可以有多个类名
  • 因为有{}包裹,所以称为对象形式,本质不是
<span :class="{ style3: 1 == 1 }"></span>
  • class中的style3有没有,取决于后面的计算结果,结果为true则有,flase则没有

浏览器源码:
在这里插入图片描述

4. :class=“[类名1,类名2]”

建议使用这种形式

  1. 可以是1个或者多个类名
  2. 类名可以是响应式对象
  3. 还可以三目运算
  • 目的:可以有多个类名
const a = ref("style4")
const b = ref("style5")
<span :class="[a,b]"></span>

浏览器源码:
在这里插入图片描述

  • 优点:可以使用三目运算符
const a = ref("style4");
const b = ref("style5");
const c = ref("style6");<span :class="[1 == 1 ? a : b, c]"></span>

浏览器源码:
在这里插入图片描述

实际中的应用

  • 实现标签切换

代码中重要的地方

  1. 通过:class实现类的添加和删除,达到切换样式的效果
  2. 动态组件<component :is="组件名字"></component>

父组件代码:

父组件App.vue
<template><component :is="whichVue"></component><!-- active:当前选中的标签的索引,被选中时的样式--><span:class="[active === index ? 'active' : '']"class="vueTitle"@click="chooseVue(item, index)"v-for="(item, index) in data">{{ item.name }}</span><!-- 动态组件,由is的值来决定<component>的位置渲染哪一个的组件 --></template><script setup lang="ts">
import { ref,shallowRef } from "vue";
import SonA from "./components/A.vue";
import SonB from "./components/B.vue";
import SonC from "./components/C.vue";// 动态组件is的值
const whichVue = shallowRef(SonA);
// 点击选择标签后,active表示被选中标签的索引
const active = ref(0);
// 点击事件:标签被点击后,更新whichVue和active的值
const chooseVue = (item: { name: string; com: any }, index: number) => {whichVue.value = item.com;active.value = index;
};
// 把子组件的名字和组件绑定在一起
const data = shallowRef([{name: "A组件",com: SonA,},{name: "B组件",com: SonB,},{name: "C组件",com: SonC,},
]);
</script><style lang="scss">
.active {background-color: blue;
}
.vueTitle {margin: 10px;padding: 5px 10px;border: 1px solid #ccc;cursor: pointer;
}
</style>

子组件A的代码:

<template><div class="com">子组件A.vue的内容</div>
</template><script setup lang="ts"></script><style scoped lang="scss">
@import "../style.scss";
.com{@include sonVueStyle;
}
</style>

子组件B的代码

<template><div class="com">子组件B.vue的内容</div>
</template><script setup lang="ts">
</script><style scoped lang="scss">
@import "../style.scss";
.com {@include sonVueStyle;
}
</style>

子组件C的代码:

<template><div class="com">子组件C.vue的内容</div>
</template><script setup lang="ts"></script><style scoped lang="scss">
@import "../style.scss";
.com{@include sonVueStyle;
}
</style>

子组件的样式:

@mixin sonVueStyle{height:100px;border:2px solid #ccc;font-size:20px;display: flex;justify-content:center;align-items: center;width: 300px;margin: 10px;
}

在这里插入图片描述

相关文章:

  • java后端请求与响应总结
  • C++入门基础知识31
  • Vue解决父子组件传值,子组件改变值后父组件的值也改变的问题
  • WPF—Triggers触发器
  • Linux驱动开发基础(SR501人体红外模块)
  • HashMap 的实现原理
  • CSS3动画——飞行的小精灵
  • webpark 如何将本地访问地址http://localshot:3000修改为自己需要的访问地址https://www.example.com:3000
  • 【ES6】使用Proxy实现单例模式
  • 论文阅读:scMGCA----模型方法
  • Go语言Time包的使用
  • C. Propagating tree
  • HTML5 浏览器支持
  • XML CSS:结构和样式的完美结合
  • 秋招突击——8/16——字节广告业务——面经整理——二面挂
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 【Linux系统编程】快速查找errno错误码信息
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • Angular数据绑定机制
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • CSS相对定位
  • ES6之路之模块详解
  • exports和module.exports
  • Java超时控制的实现
  • php的插入排序,通过双层for循环
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Terraform入门 - 1. 安装Terraform
  • 对象引论
  • 我建了一个叫Hello World的项目
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • mysql面试题分组并合并列
  • #pragma once与条件编译
  • $jQuery 重写Alert样式方法
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (7)svelte 教程: Props(属性)
  • (9)STL算法之逆转旋转
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (办公)springboot配置aop处理请求.
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (面试必看!)锁策略
  • (四)c52学习之旅-流水LED灯
  • (图)IntelliTrace Tools 跟踪云端程序
  • (五)MySQL的备份及恢复
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .NET C# 使用GDAL读取FileGDB要素类
  • .NET Core引入性能分析引导优化
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • [ C++ ] STL---stack与queue
  • [ Python ]使用Charles对Python程序发出的Get与Post请求抓包-解决Python程序报错问题
  • [@Controller]4 详解@ModelAttribute
  • [BUG] Authentication Error