VUE style scoped 内修改组件样式

style scoped 内修改外部组件样式不生效的解决方法

想修改项目中 ant Design 组件的样式,发现在< style scoped> 内直接修改并没有效果。

解决方法

当vue的< style>标签有 scoped 属性时,它的 CSS 只作用于当前组件中的元素。如果需要修改子组件的样式可以用到深度作用选择器
如果希望 scoped 样式中的一个选择器能够作用得“更深”,影响到子组件,你可以使用 >>> 操作符:

// 在css 中使用
<style scoped>
.a >>> .b {  
     width:100px;
    text-align: center; 
    }
</style>

有些像 Sass 之类的预处理器无法正确解析 >>>。这种情况下你可以使用 /deep/ 或 ::v-deep 操作符取而代之——两者都是 >>> 的别名,同样可以正常工作。

// 在sass 中使用
<style lang="scss" scoped>
.a {
  /deep/  .b {
    width:100px;
    text-align: center;
  }
}
</style>

参考官方文档:https://vue-loader.vuejs.org/zh/guide/scoped-css.html#深度作用选择器

原因

scope设计的初衷就是让样式变得不可修改。当设置了scoped的style标签无法覆盖最外面antd的样式
scoped原理就是在css以及html上加一个唯一标识,然后根据权重来实现目的
如下

//
<style scoped>
.example {
  color: red;
}
</style>

<template>
  <div class="example">hi</div>
</template>

转换结果为

//
<style>
.example[data-v-f3f3eg9] {
  color: red;
}
</style>

<template>
  <div class="example" data-v-f3f3eg9>hi</div>
</template>

其他方法

可以在一个组件中同时使用有 scoped 和非 scoped 样式:

// 混合样式
<style>
/* 全局样式 */
</style>

<style scoped>
/* 本地样式 */
</style>
### Vue.js 中 scoped CSS 的工作原理 在 Vue 组件中使用 `scoped` 属性可以确保样式仅应用于当前组件内的 HTML 元素,而不会影响全局或其他组件中的元素。当 `<style>` 标签带有 `scoped` 属性时,Vue 编译器会自动为该组件下的所有 DOM 节点添加唯一的属性,并相应修改 CSS 选择器来匹配这些特定的属性[^3]。 例如,在一个名为 `MyComponent.vue` 文件内: ```html <template> <div class="example"> <p>This is a paragraph.</p> </div> </template> <style scoped> .example { color: red; } </style> ``` 编译后的实际效果类似于这样(假设唯一标识符为 `_v-f3f3eg9`): ```html <div class="example" data-v-f3f3eg9=""> <p data-v-f3f3eg9="">This is a paragraph.</p> </div> ``` 对应的 CSS 变成如下形式: ```css .example[data-v-f3f3eg9] { color: red; } ``` 这种机制有效地防止了不同组件之间的样式冲突问题,同时也使得开发者能够更加直观地理解样式的应用范围和层次结构[^4]。 ### 实际案例展示 考虑这样一个场景:父级组件 Parent 和 子级组件 Child 都有各自的 `.title` 类名用于设置标题文字的颜色。如果不加任何限定的话,默认情况下两个类都会被加载到页面上并可能造成样式覆盖的问题;但如果给定其中一个或两者都加上 `scoped` 后,则各自只会影响自己所属的部分而不干扰对方。 #### 不带 `scoped` 的情况 ```html <!-- Parent Component --> <template> <section> <h1 class="title">Parent Title</h1> <!-- Include child component here --> </section> </template> <style> .title { /* Global style */ color: blue; } </style> ``` ```html <!-- Child Component --> <template> <article> <h2 class="title">Child Title</h2> </article> </template> <style> .title {/* This will also be global without &#39;scoped&#39;*/ color: green; } </style> ``` 在这种配置下,“Child Title”的颜色会被渲染为绿色,因为它的定义位于后面并且具有相同的优先级级别,从而覆盖掉了前面的蓝色声明。 #### 带 `scoped` 的情况 现在我们把上面的例子稍微调整一下,让子组件内部的样式带上 `scoped` 关键字: ```html <!-- Child Component with scoped styles --> <template> <article> <h2 class="title">Scoped Child Title</h2> </article> </template> <style scoped> .title{/* Only applies within this component due to &#39;scoped&#39; attribute */ color: orange; } </style> ``` 此时,“Scoped Child Title”将会显示橙色字体,因为它受到局部作用域保护,即使存在相同名称的选择器也不会受到影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值