Vue style里面使用@import引入外部css, 作用域是全局的解决方案
2024-10-19 14:35:07
问题描述
使用@import引入外部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引进来,多次请求浪费不必要的资源。
@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>
参考链接
原文地址:https://segmentfault.com/a/1190000012728854
最新文章
- ES6模块import细节
- 在web中使用windows控件,实现摄像头功能
- Shell 脚本
- iOS 模仿微信的照片选择器
- Json-转换
- iOS开发-网易滚动导航栏
- # 图解TCP/IP读书笔记(五)
- HDU 4891 The Great Pan (模拟)
- java验证码Captcha
- 一个IP支持多个网站实例(Apache2、Ubuntu相关)
- 使用Unity开发HoloLens应用
- js---疑点代码段解析
- [Android FrameWork 6.0源码学习] LayoutInflater 类分析
- 回味Python2.7——笔记3
- Servlet生命周期与工作原理(转载)
- 从外面更新unity需要用的题库
- linux内核里的字符串转换 ,链表操作常用函数(转)
- vue.js computed,watch的区别
- 【NOIP 2015】Day2 T3 运输计划
- Beta阶段——第6篇 Scrum 冲刺博客
热门文章
- node——underscore的使用
- CF487E Tourists(圆方树+堆+链剖)
- 解决locate无法使用的问题
- 【转】C# winform 加载网页 模拟键盘输入自动接入访问网络
- ASP.NET-MVC中Entity和Model之间的关系
- urlEncoder和urlDecoder的作用和使用
- System.getProperty()方法可以获取的值
- eclipse启动错误:An internal error occurred during: ";reload maven project";.
- WPF学习(一) - XAML
- Pyinstaller 0