3 回答

TA貢獻1982條經驗 獲得超2個贊
我將使用Shadow DOM和Light DOM術語回答您的問題(它來自Web組件,請參見此處)。一般來說:
影子DOM-是組件的內部DOM,由您定義(作為組件的創建者)并向最終用戶隱藏。例如:
@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 { /* ... */ }
因此,您的問題的答案非常簡單:
@ViewChildren和之間的區別@ContentChildren是,@ViewChildren在Shadow DOM 中查找元素,而@ContentChildren在Light DOM 中查找元素。

TA貢獻1876條經驗 獲得超5個贊
正如其名,@ContentChild
并@ContentChildren
查詢將返回現有的內部指令<ng-content></ng-content>
視圖的元素,而@ViewChild
并@ViewChildren
不僅要看直接對你的視圖模板元素。
- 3 回答
- 0 關注
- 1062 瀏覽
添加回答
舉報