Vue style里面使用@import引入外部css, 作用域是全局的解决方案

2020-05-06 18:46:06

错误的做法

<style>

@import "../static/css/user.css";

</style>

正确的做法


我们只需把@import改成<style src=""></style>引入外部样式,就可以解决样式是全局的问题

参考地址 Vue style里面使用@import引入外部css, 作用域是全局的解决方案

使用@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>


  • 2020-12-29 14:35:12

    git 放弃所有修改

    此命令用来放弃掉所有还没有加入到缓存区(就是 git add 命令)的修改:内容修改与整个文件删除。但是此命令不会删除掉刚新建的文件。因为刚新建的文件还没已有加入到 git 的管理系统中。所以对于git是未知的。自己手动删除就好了。

  • 2020-12-29 16:20:29

    使用console进行 性能测试 和 计算代码运行时间

    对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用 debugger 会显得过于笨重,最常用的方法是会将值输出到控制台上方便调试。 最常用的语句就是console.log(expression)了。

  • 2021-01-05 15:41:42

    nodejs修改时区

    ​let date = new Date(); date.setHours(date.getHours() + 8);

  • 2021-01-06 23:09:38

    mp3解码器转PCM合并

    首先,为了混合两个音频文件,您需要操纵它们的原始表示;由于MP3文件被压缩,您无法直接访问信号的原始表示.您需要对压缩的MP3流进行解码,以便“理解”您的音频信号的波形,然后可以混合使用.