3 回答

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
我將使用Shadow DOM和Light DOM術(shù)語(yǔ)回答您的問(wèn)題(它來(lái)自Web組件,請(qǐng)參見(jiàn)此處)。一般來(lái)說(shuō):
影子DOM-是組件的內(nèi)部DOM,由您定義(作為組件的創(chuàng)建者)并向最終用戶隱藏。例如:
@Component({
selector: 'some-component',
template: `
<h1>I am Shadow DOM!</h1>
<h2>Nice to meet you :)</h2>
<ng-content></ng-content>
`;
})
class SomeComponent { /* ... */ }
輕型DOM-是組件的最終用戶提供給組件的DOM。例如:
@Component({
selector: 'another-component',
directives: [SomeComponent],
template: `
<some-component>
<h1>Hi! I am Light DOM!</h1>
<h2>So happy to see you!</h2>
</some-component>
`
})
class AnotherComponent { /* ... */ }
因此,您的問(wèn)題的答案非常簡(jiǎn)單:
@ViewChildren和之間的區(qū)別@ContentChildren是,@ViewChildren在Shadow DOM 中查找元素,而@ContentChildren在Light DOM 中查找元素。

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
正如其名,@ContentChild
并@ContentChildren
查詢將返回現(xiàn)有的內(nèi)部指令<ng-content></ng-content>
視圖的元素,而@ViewChild
并@ViewChildren
不僅要看直接對(duì)你的視圖模板元素。
- 3 回答
- 0 關(guān)注
- 1061 瀏覽
添加回答
舉報(bào)