当 <style>
标签有 scoped
属性时,至关于在元素中添加了一个惟一属性用来区分。css
<style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </template>
它经过使用 PostCSS 来实现如下转换,转换结果:html
<style> .example[data-v-f3f3eg9] { color: red; } </style> <template> <div class="example" data-v-f3f3eg9>hi</div> </template>
经过给样式名加hash字符串后缀的方式,实现特定做用域语境中的样式编译后的样式在全局惟一。vue
<template> <p :class="$style.gray"> Im gray </p> </template> <style module> .gray { color: gray; } </style>
使用module的结果编译以下:ide
<p class="gray_3FI3s6uz">Im gray</p> .gray_3FI3s6uz { color: gray; }
因而可知,css module直接替换了类名,排除了用户设置类名影响组件样式的可能性。ui
推荐使用CSS Modulesspa
详细见官方文档:https://vue-loader.vuejs.org/zh/guide/scoped-css.html#混用本地和全局样式code