局部作用域内(scoped)使用@import引入css引发的css全局污染的问题(The problem of CSS global pollution caused by introducing CSS with @ import in local scope)

在项目中我们可以使用下面这种方式引入外部的css文件

<style src='./main.css' scoped></style>

  也可以使用下面这种方式

<style scoped>
 @import './main.css' 
</style>

  但是这种方式 scoped不会生效 也就是通过@import引入的文件打包后会变成全局的

css-loader对import的文件会当做外部资源,那么我能理解为它是把import的css文件单独提取出来,并没有把其中的样式放在<style scoped>中解析,而是最后把<style>中计算结果和import的css文件混合后,交由style-loader最后解析为style标签加载在页面

<style scoped>
    @import '../../assets/css/home.css';  
</style>
/*这样写的话import的css文件会被编译为全局样式,但是引入less等预编译文件,就会局部生效*/
————————

In the project, we can use the following method to import external CSS files

<style src='./main.css' scoped></style>

You can also use the following method

<style scoped>
 @import './main.css' 
</style>

However, scoped will not take effect in this way, that is, the files imported through @ import will become global after packaging

CSS loader will act as an external resource for the imported file, so I can understand that it extracts the imported CSS file separately and does not put the style in & lt; style scoped> Instead of parsing it, put & lt; style> After the calculation result is mixed with the imported CSS file, it is handed over to the style loader and finally parsed into a style tag and loaded on the page

<style scoped>
    @import '../../assets/css/home.css';  
</style>
/*这样写的话import的css文件会被编译为全局样式,但是引入less等预编译文件,就会局部生效*/