這篇文章為大家帶來了關於Slots的相關知識,Slots 的作用就是給模板元素傳值,增強模板元素的靈活性和通用性,希望對大家有幫助。
熟悉 Vue 的同學應該都知道」插槽(slot)「的概念,透過使用插槽可以讓頁面內容的組織更加靈活。
在 Web Components 體系中也有插槽的概念,今天我們就來具體了解一下 Slots,本文主要包含以下內容:
- ##為什麼要用 Slots ?
- Slots 的相關特性
<template> <p>MY CARD</p> <p> My name is 编程三昧。 </p></template>既然是模板,那就意味著在很多地方都會使用到它,但是,這裡會存在一個問題:
所有使用這個模板的地方都將顯示模板中的內容,即並不是所有人的名字都叫”編程三昧“ 。
在這種情況下,叫其他名字的人是沒法使用這個模板的,顯然,這就和使用模板的初衷相違背了,這個模板的使用範圍太過狹小,不存在通用性。 想要使得這個範本具有通用性,其關鍵點在於.details 中顯示的內容是否具有通用性。
<!--在模板中使用 slot 进行占位--><template> <p>MY CARD</p> <p> My name is <slot>编程三昧</slot>。 </p></template><!--在使用上面模板的自定义元素中给 slot 传值--><my-card> <span>插槽传值</span></my-card><my-card> <span>web Components</span></my-card>其對應的JS 程式碼如下:
class MyCard extends HTMLElement { constructor () { super(); const template = document.getElementById('cardTmp'); const templateContent = template.content; this.attachShadow({mode: 'open'}).appendChild( templateContent.cloneNode(true) ); }}customElements.define('my-card', MyCard);實現效果:
Slots 的作用就是給模板元素傳值,增強模板元素的彈性和通用性。
Slots 的相關特性對於 Slots 的相關特性,我透過問答的形式逐一解釋。 Slots 的 name 屬性有什麼作用? 帶有指定 name 的 Slots 稱為 」具名插槽“,name 是 slot 的唯一標識。 在引入插槽內容的元素上需要使用與Slots.name 值相同的 slot 屬性。看下面的程式碼:
<template> <p>MY CARD</p> <p> My name is <slot>19</slot>。 </p></template><my-card> <span>编程三昧</span></my-card><my-card> <span>web Components</span></my-card><script> class MyCard extends HTMLElement { constructor () { super(); const template = document.getElementById('cardTmp'); const templateContent = template.content; this.attachShadow({mode: 'open'}).appendChild( templateContent.cloneNode(true) ); } } customElements.define('my-card', MyCard);</script>運行效果:
傳值時的 slot 屬性值必須和 Slots 的 name 屬性值一致。不傳值給 Slots 會怎樣? 將上面兩個自訂元素
my-card 中的span 元素去掉,不傳任何值,也就是改成這樣:
<my-card></my-card>運行後的效果:
如果不給Slots 傳值,那麼Slots 會顯示它自己預設的內容。
其實結合以上兩點,還可以得到一個結論:如果有引用 Slots ,那就只有對應 name 的 Slots 內容會被顯示,其餘的 Slots 都不顯示。
正常 DOM 中可以使用 Slots 嗎? 這裡的」正常 DOM「 是相對於 Shadow DOM 來說的,指的是頁面所在的文件物件。 程式碼如下:<slot>Slots 预设值</slot><p>bcsm</p>顯示如下:
正常DOM 中使用Slots,它會直接渲染在頁面上,切不具備插槽效果。
Slots 是不是必須用在 Templates 中? 我們前面看到的例子中,Slots 是在 Templates 中,那是不是意味著 Slots 必須用在 Templates 中才能生效呢? 因為已經驗證過在正常DOM 中的Slots 是無效的,所以我們在Shadow DOM 中做個測試,程式碼如下:<h1 id="不在-Templates-中使用-Slots">不在 Templates 中使用 Slots</h1> <p> <slot>这是 Slots 预设值</slot> </p> <my-paragraph> <span>编程三昧</span> </my-paragraph> <script> class MyParagraph extends HTMLElement { constructor () { super(); const template = document.getElementById('templ'); this.attachShadow({mode: 'open'}).appendChild( template.cloneNode(true) ); } } customElements.define('my-paragraph', MyParagraph); </script>顯示效果如下:
Slots 在 Shadow DOM 中就可生效,並非一定要用在 Templates 中。
一個自訂元素中可以使用多個同名 Slots 嗎? 看程式碼:<template> <p>MY CARD</p> <p> My name is <slot>编程三昧</slot>。 </p></template><my-card> <span>插槽传值1</span> <span>插槽传值2</span></my-card><script> class MyCard extends HTMLElement { constructor () { super(); const template = document.getElementById('cardTmp'); const templateContent = template.content; this.attachShadow({mode: 'open'}).appendChild( templateContent.cloneNode(true) ); } } customElements.define('my-card', MyCard);</script>顯示效果:
一個Slots 可以接收多個傳入值,且都會解析顯示出來。
Slots 的传值元素必须是自定义元素的直接子元素吗?
上面的例子中,所有给 Slots 传值的元素都是自定义元素的子元素,那是不是非直接子元素不行呢?
代码如下:
<template> <p>MY CARD</p> <p> My name is <slot>编程三昧</slot>。 </p></template><my-card> <p> <span>插槽传值1</span> </p></my-card><script> class MyCard extends HTMLElement { constructor () { super(); const template = document.getElementById('cardTmp'); const templateContent = template.content; this.attachShadow({mode: 'open'}).appendChild( templateContent.cloneNode(true) ); } } customElements.define('my-card', MyCard);</script>
运行效果(传值失效):
结论:给 Slots 传值的元素必须是自定义元素的直接子元素,否则传值失效。
更多编程相关知识,请访问:编程视频!!
以上是一起聊聊Web Components之Slots(實例詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

使用ID選擇器在CSS中並非固有地不好,但應謹慎使用。 1)ID選擇器適用於唯一元素或JavaScript鉤子。 2)對於一般樣式,應使用類選擇器,因為它們更靈活和可維護。通過平衡ID和類的使用,可以實現更robust和efficient的CSS架構。

html5'sgoalsin2024focusonrefinement和optimization,notNewFeatures.1)增強performanceandeffipedroptimizedRendering.2)inviveAccessibilitywithRefinedwithRefinedTributesAndEllements.3)explityconcerns,尤其是withercercern.4.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供應,2)語義結構,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,簡化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

classSelectorSareVersAtileAndReusable,whileIdSelectorSareEctorAreNiqueAndspecific.1)USECLASSSELECTORS(表示)forStylingmultilemtsswithsharedCharacteristics.2)UseIdSelectors.2)UseIdSelectors(eustotedBy#)

IDSareuniqueIdentifiersForsingLelements,而LileclassesstyLemultiplelements.1)useidsforuniquelementsand andjavascripthooks.2)useclassesforporporporblesable,flexiblestylestylestylinglingactossmultiplelements。

使用僅類選擇器可以提高代碼的重用性和可維護性,但需要管理類名和優先級。 1.提高重用性和靈活性,2.組合多個類創建複雜樣式,3.可能導致冗長類名和優先級問題,4.性能影響微小,5.遵循最佳實踐如簡潔命名和使用約定。

ID和class選擇器在CSS中分別用於唯一和多元素的樣式設置。 1.ID選擇器(#)適用於單一元素,如特定導航菜單。 2.Class選擇器(.)用於多元素,如統一按鈕樣式。應謹慎使用ID,避免過度特異性,並優先使用class以提高樣式複用性和靈活性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境