簡體   English   中英

Vue中迭代組件使用scoped樣式好不好

[英]Is it good to use scoped style for iterated component in Vue

我們在 Vue 的組件中使用 scoped 樣式,它有優勢。 但是假設我有一個像 BoxComponent.vue 這樣的組件並定義了一個 class。

<style scoped>
.box-component{
  float: left;
  flex-grow: initial !important;
  margin: 0 3px;
}
</style>

我認為一次或兩次使用它可能會很好,但是當我在循環中使用它時可能會循環數百次,然后它將生成獨立的樣式類,因為vue-loader會以不同的方式處理每個項目。

在那種情況下,我認為這可能是速度和 memory 用法方面的問題。 有人可以詳細說明嗎? Vue.js什么時候用scoped style好還是global style好?

IMO,這個問題應該更像是一個

我是否需要我的風格在這里是全球性的?

比性能的。 我會說它總體上毫無意義,特別是如果您使用實用程序框架(如 Tailwind)。 事實上,您將在所有頁面上獲得更少的 CSS 次重新定義。

選擇如何編寫 CSS 以及具體使用什么,將比是否確定范圍產生更大的影響。 如果您對保持超薄的東西感興趣,請閱讀像UnoCSS這樣的超輕工具。

此外,第三方腳本(如 GTM、小部件聊天或公司強制添加的任何內容)對性能的影響會大得多。

我不認為運行時 CSS 會讓你的性能很差,只是不要在你的網站上使用像*div這樣的超級通用選擇器,你應該沒問題。

盡管如此,如果你有一個超大的網站,最好通過檢查總渲染時間來嘗試以某種方式對整個網站進行基准測試(當然要關注樣式指標): https://developer.chrome.com/docs /devtools/評估性能/


官方來源: https://vue-loader.vuejs.org/guide/scoped-css.html#child-component-root-elements

我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style>

[英]Can I have the className of a leaflet divIcon inside the <style scoped> section of a vue component?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何將 styles 傳遞給子組件並將其用作 Vue 中的作用域樣式? 在 Vue 組件中使用 CSS 變量(作用域) 如何在VUE Component的Scoped區域使用JSON數據 在Vue的組件腳本中使用基於插槽的數據 我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style> vue.js中的范圍樣式根本沒有范圍 Vue:從范圍插槽中的組件獲取方法 Vue:是否可以導入僅適用於該組件的js 如何有條件地在范圍內的 Vue 組件中附加一個元素? 在 Vue.js 中為子組件中的迭代槽迭代槽內容
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM