亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

@ViewChild和@ContentChild有什么區別?

@ViewChild和@ContentChild有什么區別?

互換的青春 2019-11-22 15:06:46
角2提供@ViewChild,@ViewChildren,@ContentChild和@ContentChildren用于查詢一個組件的派生元素裝飾器。前兩個和后兩個有什么區別?
查看完整描述

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 中查找元素。


查看完整回答
反對 回復 2019-11-22
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

正如其名,@ContentChild@ContentChildren查詢將返回現有的內部指令<ng-content></ng-content>視圖的元素,而@ViewChild@ViewChildren不僅要看直接對你的視圖模板元素。


查看完整回答
反對 回復 2019-11-22
  • 3 回答
  • 0 關注
  • 1062 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號