css一直有个使人困扰的做用域问题:即便是模块化编程下,在对应的模块的js中import css进来,这个css仍然是全局的。为了不css样式之间的污染,vue中引入了scoped这个概念。css
在vue文件中的style标签上,有一个特殊的属性:scoped。当一个style标签拥有scoped属性时,它的CSS样式就只能做用于当前的组件。经过设置该属性,使得组件之间的样式不互相污染。若是一个项目中的全部style标签所有加上了scoped,至关于实现了样式的模块化。vue
可是这些样式又是如何实现不相互影响呢?编程
vue中的scoped 经过在DOM结构以及css样式上加惟一不重复的标记:data-v-hash的方式,以保证惟一(而这个工做是由过PostCSS转译实现的),达到样式私有化模块化的目的。bash
总结一下scoped三条渲染规则:dom
上个栗子。转译前:模块化
<style lang="scss" scoped>
.test {
background: blue;
span{
color:red;
}
}
</style>
<template>
<div class="test">
<span>hello world !</span>
</div>
</template>
复制代码
转译后:ui
<style lang="css">
.test[data-v-ff86ae42] {
background: blue;
}
.test span[data-v-ff86ae42]{
color: red;
}
</style>
<template>
<div class="test" data-v-ff86ae42>
<span data-v-ff86ae42>hello world !</span>
</div>
</template>
复制代码
能够看出:PostCSS会给一个组件中的全部dom添加了一个独一无二的动态属性data-v-xxxx,而后,给CSS选择器额外添加一个对应的属性选择器来选择该组件中dom,这种作法使得样式只做用于含有该属性的dom——组件内部dom, 从而达到了'样式模块化'的效果.spa
可是,在作项目中,会遇到这么一个问题,即:引用了第三方组件,须要在组件中局部修改第三方组件的样式,而又不想去除scoped属性形成组件之间的样式污染。那么有哪些解决办法呢?code
<style lang="scss">
/*添加要覆盖的样式*/
</style>
<style lang="scss" scoped>
/* local styles */
</style>
<!--vue官网中提到:一个 .vue 文件能够包含多个style标签。因此上面的写法是没有问题的。-->
复制代码
<template>
<div class="box">
<dialog></dialog>
</div>
</template>
<!--使用 >>>或者 /deep/ 操做符(Sass 之类的预处理器没法正确解析 >>>,可使用/deep/)-->
<style lang="scss" scoped>
.box {
/deep/ input {
width: 166px;
text-align: center;
}
}
</style>
或者
<style lang="scss" scoped>
.box >>> input {
width: 166px;
text-align: center;
}
}
</style>
复制代码
但愿能帮助遇到一样问题的你,thanks!作用域