How to implement scoped in vue css?

as the title

Mar.19,2021
The effect of the scoped attribute in

vue is actually mainly achieved through PostCSS translation. There is a lib folder in the
vue-loader file and there is a style-compiler folder under it, and the index.js in it is for css. What is loaded in

is that PostCss,PostCSS adds a unique dynamic property to all dom in the component, and then adds an additional corresponding property selector to the CSS selector to select the dom, principle in the component using css's property selector.


It is explained in the

document that the basic principle is that when vue loader loads scoped css, it transforms

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

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

convert to:

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

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

when you debug with chrome, you can see it very clearly.


upstairs makes it clear that tag is used to define parts. The tag of each page is unique, and even the same classes on different pages will not overlap because of different tag.
if you ask if you generate or if you add tag to html, you need to look at the vue-loader source


if scoped is added, the corresponding selector and the corresponding element are bound to an attribute generated by a globally unique string at compile time.

add:
the landlord gave the first three answers "no help", right?

I can only say that this kind of behavior is stupid!

Menu