使用@import引入外部css,做用域倒是全局的css
<template> </template> <script> export default { name: "user" }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> @import "../static/css/user.css"; .user-content{ background-color: #3982e5; } </style>
Add "scoped" attribute to limit CSS to this component only
这句话你们应该是见多了, 我也使用scoped, 可是使用@import引入外部样式表做用域依然是全局的,看了一遍@import的规则后, 进行初步猜想,难道是@import引入外部样式表错过了scoped style?前端
又回想到此前看过的前端性能优化文章里面都有提到,在生产环境中不要使用@import引入css,由于在请求到的css中含有@import引入css的话,会发起请求把@import的css引进来,屡次请求浪费没必要要的资源。vue
@import并非引入代码到<style></style>里面,而是发起新的请求得到样式资源,而且没有加scoped性能优化
<style scoped> @import "../static/css/user.css"; </style>
咱们只需把@import改为<style src=""></style>引入外部样式,就能够解决样式是全局的问题前端性能
<style scoped src="../static/css/user.css"> <style scoped> .user-content{ background-color: #3982e5; } </style>
总体代码以下:性能
<template> </template> <script> export default { name: "user" }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped src="../static/css/user.css"> <style scoped> .user-content{ background-color: #3982e5; } </style>