搜尋
首頁web前端前端問答一起聊聊Web Components之Slots(實例詳解)

這篇文章為大家帶來了關於Slots的相關知識,Slots 的作用就是給模板元素傳值,增強模板元素的靈活性和通用性,希望對大家有幫助。

一起聊聊Web Components之Slots(實例詳解)

熟悉 Vue 的同學應該都知道」插槽(slot)「的概念,透過使用插槽可以讓頁面內容的組織更加靈活。

在 Web Components 體系中也有插槽的概念,今天我們就來具體了解一下 Slots,本文主要包含以下內容:

    ##為什麼要用 Slots ?
  • Slots 的相關特性
Slots 的作用

我們先來看一個模板元素:

<template>
    <p>MY CARD</p>
    <p>
        My name is 编程三昧。    </p></template>
既然是模板,那就意味著在很多地方都會使用到它,但是,這裡會存在一個問題:

所有使用這個模板的地方都將顯示模板中的內容,即並不是所有人的名字都叫”編程三昧“ 。

在這種情況下,叫其他名字的人是沒法使用這個模板的,顯然,這就和使用模板的初衷相違背了,這個模板的使用範圍太過狹小,不存在通用性。

想要使得這個範本具有通用性,其關鍵點在於

.details 中顯示的內容是否具有通用性。

開動腦筋想一想,我們是不是可以將其中的」程式設計三昧「設為動態內容,誰使用這個模板,誰就傳入自己的名字。恰好, Slots(插槽)就可以實現這種效果,具體如下:

<!--在模板中使用 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);
實現效果:

一起聊聊Web Components之Slots(實例詳解)

透過上面的例子,我們可以用一句話總結Slots 的作用:

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(&#39;cardTmp&#39;);
            const templateContent = template.content;

            this.attachShadow({mode: &#39;open&#39;}).appendChild(
                templateContent.cloneNode(true)
            );
        }
    }
    customElements.define(&#39;my-card&#39;, MyCard);</script>
運行效果:

一起聊聊Web Components之Slots(實例詳解)

因為傳入的slot 屬性值和Slots 的name 屬性值對不上,所以Slots 未被插入。

傳值時的 slot 屬性值必須和 Slots 的 name 屬性值一致。

不傳值給 Slots 會怎樣?

將上面兩個自訂元素

my-card 中的span 元素去掉,不傳任何值,也就是改成這樣:

<my-card></my-card>
運行後的效果:

一起聊聊Web Components之Slots(實例詳解)

可以看到,

如果不給Slots 傳值,那麼Slots 會顯示它自己預設的內容

其實結合以上兩點,還可以得到一個結論:

如果有引用 Slots ,那就只有對應 name 的 Slots 內容會被顯示,其餘的 Slots 都不顯示

正常 DOM 中可以使用 Slots 嗎?

這裡的」正常 DOM「 是相對於 Shadow DOM 來說的,指的是頁面所在的文件物件。

程式碼如下:

<slot>Slots 预设值</slot><p>bcsm</p>
顯示如下:

一起聊聊Web Components之Slots(實例詳解)

#總結:

正常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(&#39;templ&#39;);

                this.attachShadow({mode: &#39;open&#39;}).appendChild(
                    template.cloneNode(true)
                );
            }
        }
        customElements.define(&#39;my-paragraph&#39;, MyParagraph);
    </script>
顯示效果如下:

一起聊聊Web Components之Slots(實例詳解)

#從顯示效果可以看到,將包含Slots 的正常DOM 節點在追加到Shadow DOM 後,Slots 顯示傳入的值,也就是說Slots 是生效了的。

總結:

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(&#39;cardTmp&#39;);
            const templateContent = template.content;

            this.attachShadow({mode: &#39;open&#39;}).appendChild(
                templateContent.cloneNode(true)
            );
        }
    }
    customElements.define(&#39;my-card&#39;, MyCard);</script>
顯示效果:

一起聊聊Web Components之Slots(實例詳解)

#結論:

一個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(&#39;cardTmp&#39;);
            const templateContent = template.content;

            this.attachShadow({mode: &#39;open&#39;}).appendChild(
                templateContent.cloneNode(true)
            );
        }
    }
    customElements.define(&#39;my-card&#39;, MyCard);</script>

运行效果(传值失效):

一起聊聊Web Components之Slots(實例詳解)

结论:给 Slots 传值的元素必须是自定义元素的直接子元素,否则传值失效

更多编程相关知识,请访问:编程视频!!

以上是一起聊聊Web Components之Slots(實例詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:CSDN。如有侵權,請聯絡admin@php.cn刪除
CSS:使用ID選擇器不好嗎?CSS:使用ID選擇器不好嗎?May 13, 2025 am 12:14 AM

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

HTML5:2024年的目標HTML5:2024年的目標May 13, 2025 am 12:13 AM

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

HTML5試圖改進的主要領域是什麼?HTML5試圖改進的主要領域是什麼?May 13, 2025 am 12:12 AM

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

CSS ID和類:常見錯誤CSS ID和類:常見錯誤May 13, 2025 am 12:11 AM

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

課程和ID選擇器之間的差異是什麼?課程和ID選擇器之間的差異是什麼?May 12, 2025 am 12:13 AM

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

CSS IDS vs類:真正的差異CSS IDS vs類:真正的差異May 12, 2025 am 12:10 AM

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

CSS:如果我只使用課程怎麼辦?CSS:如果我只使用課程怎麼辦?May 12, 2025 am 12:09 AM

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

CSS中的ID和類選擇器:初學者指南CSS中的ID和類選擇器:初學者指南May 12, 2025 am 12:06 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境