閱讀完這篇博客你會有如下收穫:html
The slotting algorithm assigns nodes of a shadow tree host into slots of that tree.vue
Input HOST -- a shadow tree host Output All child nodes of HOST are slottednode
When each node is assigned to a slot, this slot is also added to the node's destination insertion points list.git
這是w3c web components規範的一個提案,地址位於https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.mdgithub
在這個提案中,咱們發現shadow DOM和shadow Tree這兩個概念,關於它們的規範,在這裏:w3c.github.io/webcomponen…web
mdn上關於shadow DOM的一篇特別好的文章:developer.mozilla.org/en-US/docs/…算法
Shadow DOM : attach a hidden separated DOM to an element.bash
幾個shadow DOM的專業術語:ide
Shadow DOM知識點:this
shadow DOM的做用是什麼:加強組件內聚性。
An important aspect of web components is encapsulation — being able to keep the markup structure, style, and behavior hidden and separate from other code on the page so that different parts do not clash, and the code can be kept nice and clean.
vue demo: component.vue -> shadow host
<slot></slot>
<slot name="foo"></slot>
<slot name="bar"></slot>
複製代碼
page.vue -> shadow Tree
<span>+</span>
<span slot="foo">-</span>
<span slot="bar">2</span>
<span slot="bar">3</span>
複製代碼
渲染結果:
渲染結果與slot算法十分契合,可是較爲奇怪的是,vue的slot機制,不會生成像w3c web component 的shadow DOM。
web component shadow DOM是下面這樣: