首页 > 技术文章 > Vue中的scoped和scoped穿透

z-j-c 2020-10-26 16:58 原文

1.什么是scoped(转帖原地址: https://segmentfault.com/a/1190000015932467?utm_source=tag-newest#comment-area)

在Vue文件中的style标签上有一个特殊的属性,scoped。当一个style标签拥有scoped属性时候,它的css样式只能用于当前的Vue组件,
可以使组件的样式不相互污染。如果一个项目的所有style标签都加上了scoped属性,相当于实现了样式的模块化。

2.scoped的实现原理

Vue中的scoped属性的效果主要是通过PostCss实现的。以下是转译前的代码:

<style scoped lang="less">
    .example{
        color:red;
    }
</style>
<template>
    <div class="example">scoped测试案例</div>
</template>

转译后:

Translation:

.example[data-v-5558831a] {
  color: red;
}
<template>
    <div class="example" data-v-5558831a>scoped测试案例</div>
</template>

既:PostCSS给一个组件中的所有dom添加了一个独一无二的动态属性,给css选择器额外添加一个对应的属性选择器,来选择组件中的dom,这种做法使得样式只作用于含有该属性的dom元素(组件内部的dom)。

Both: POSTCSS adds a unique dynamic property to all the dom in a component, and adds a corresponding property selector to the CSS selector to select the dom in the component, this allows the style to be applied only to the dom element that contains the attribute (the dom inside the component) .

总结:scoped的渲染规则: Summary: SCOPED RENDERING RULES:

1.给HTML的dom节点添加一个不重复的data属性(例如: data-v-5558831a)来唯一标识这个dom 元素 Add An unduplicated data attribute (for example: Data-v-5558831a) to the HTML dom node to uniquely identify the Dom Element
2.在每句css选择器的末尾(编译后生成的css语句)加一个当前组件的data属性选择器(例如:[data-v-5558831a])来私有化样式 At the end of each CSS selector (the compiled CSS statement) , add a data property selector for the current component (for example: [ Data-v-5558831a ]) to privatize the style

3.scoped穿透3. scoped penetration

scoped看起来很好用,当时在Vue项目中,当我们引入第三方组件库时(如使用vue-awesome-swiper实现移动端轮播),需要在局部组件中修改第三方组件库的样式,而又不想去除scoped属性造成组件之间的样式覆盖。这时我们可以通过特殊的方式穿透scoped。Scoped seemed to work well, when we introduced third-party component libraries in a Vue project (such as mobile-side rotation using Vue-awesome-swiper) , we had to change the style of the third-party Component Library in the local component, you do not want to remove the scoped property to cause style overwrites between components. Then we can penetrate scoped in a special way.
stylus的样式穿透 使用>>> Stylus style penetrates, using > > >

外层 >>> 第三方组件 
样式
.wrapper >>> .swiper-pagination-bullet-active
background: #fff

sass和less的样式穿透 使用/deep/ The SASS and less styles penetrate, USING/DEEP/

外层 /deep/ 第三方组件 {
样式
}
.wrapper /deep/ .swiper-pagination-bullet-active{
background: 
}

 

3.在组件中修改第三方组件库样式的其它方法

上面我们介绍了在使用scoped 属性时,通过scopd穿透的方式修改引入第三方组件库样式的方法,下面我们介绍其它方式来修改引入第三方组件库的样式

在vue组件中不使用scoped属性,

在vue组建中使用两个style标签,一个加上scoped属性,一个不加scoped属性,把需要覆盖的css样式写在不加scoped属性的style标签里

建立一个reset.css(基础全局样式)文件,里面写覆盖的css样式,在入口文件main.js 中引入

 

推荐阅读