日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

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

發布時間:2023/12/2 vue 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

&lt;style scoped&gt; @import "../static/css/user.css"; &lt;/style&gt;

我們只需把@import改成<style src=""></style>引入外部樣式,就可以解決樣式是全局的問題

&lt;style scoped src="../static/css/user.css"&gt; &lt;style scoped&gt; .user-content{background-color: #3982e5; } &lt;/style&gt;

整體代碼如下:

&lt;template&gt;&lt;/template&gt;&lt;script&gt;export default {name: "user"}; &lt;/script&gt;&lt;!-- Add "scoped" attribute to limit CSS to this component only --&gt; &lt;style scoped src="../static/css/user.css"&gt; &lt;style scoped&gt; .user-content{background-color: #3982e5; } &lt;/style&gt;

參考鏈接

MDN Web技術文檔 @import

原文地址:https://segmentfault.com/a/1190000012728854


更多專業前端知識,請上 【猿2048】www.mk2048.com

總結

以上是生活随笔為你收集整理的Vue style里面使用@import引入外部css, 作用域是全局的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。