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

vue项目正确使用样式deep穿透

经常开发前端的程序员应该都知道前端一般都是组件化开发,为了避免样式污染通常会使用scoped添加属性选择器,此时如果我们想在父组件中修改子组件的样式便成了难题。其实,我们可以通过以下几种方式修改子组件样式,

  • 组件样式穿透

我们可以直接在父组件调用子组件时给组件添加样式,此方案只能添加子组件最外一层样式,如下

<!--子组件-->
<templete>
<div class="child-test1"><div class="child-test2"></div>
</div>
</templete>
<script lang="ts" setup>
defineOptions({name:"childComponent"
})
</script>
<style scoped lang="scss">
.child-test1{width:100px;height:100px;background:red;.child-test2{width:50px;height:50px;background:green;}
}
</style>
<!--父组件-->
<templete>
<div class="father-test1"><test class="father-test2"></test>
</div>
</templete>
<script lang="ts" setup>
import test from 'test.vue'
defineOptions({name:"FatherComponent"
})
</script>
<style scoped lang="scss">
.father-test1{width:200px;height:200px;.father-test2{background:yellow;}
}
</style>
  • deep样式穿透

通过deep穿透样式,我们可以更改子组件内部任意样式,如下

<!--父组件-->
<templete>
<div class="father-test1"><test></test>
</div>
<!--错误示例->
<!--<div><test  class="father-test1"></test>
</div>-->
</templete>
<script lang="ts" setup>
import test from 'test.vue'
defineOptions({name:"FatherComponent"
})
</script>
<style scoped lang="scss">
:deep(.father-test1){width:200px;height:200px;.child-test2{background:yellow;}
}
</style>

注意,使用deep的演示类不能挂载在组件上,否则不生效

http://www.lryc.cn/news/117794.html

相关文章:

  • Jenkins持续集成-快速上手
  • 查看linux 所有运行的应用和端口命令
  • Maven安装与配置,Eclipse配置Maven【图文并茂的保姆级教程】
  • 利用XLL文件投递Qbot银行木马的钓鱼活动分析
  • 2023CNSS——WEB题解(持续更新)
  • Unity之ShaderGraph 节点介绍 数学节点
  • springboot mongo 使用
  • 如何使用appuploader制作apple证书​
  • Promise详细版
  • v-for循环生成的盒子只改变当前选中的盒子的样式
  • Spring Data JPA源码
  • 如何防止CSRF攻击
  • linuxARM裸机学习笔记(7)----RTC实时时钟实验
  • NSS [UUCTF 2022 新生赛]ez_upload
  • 【操作系统】操作系统知识点总结(秋招篇)
  • 篇十九:迭代器模式:遍历集合
  • 浅谈JVM中的即时编译器(Just-In-Time compiler, JIT)
  • Android 13 Launcher——长按图标弹窗内容修改以及小组件等隐藏起来
  • 又一个不可错过的编程大模型来了让你惊呼“码农人生”不虚此行
  • 【Express.js】集成SocketIO
  • 为树莓派Pico配置交叉编译环境和工具链arm-none-eabi-gcc时可能会遇到的错误以及解决方案
  • Yum 部署K8S集群
  • 初阶C语言-操作符详解(下)
  • reposync命令——下载yum仓库中全部的包到本地
  • LC-杨辉三角
  • Golang空结构体struct{}的作用是什么?
  • 自然语言处理从入门到应用——LangChain:提示(Prompts)-[示例选择器(Example Selectors)]
  • 【实战项目】c++实现基于reactor的高并发服务器
  • Docker部署ElasticSearch7
  • 【算法|数组】滑动窗口