Angular 修改子組件的樣式


<!-- 父組件 father.html -->
<div class="father">
  <dwb-child></dwb-child>
</div>
<div class="other">
  <dwb-child></dwb-child>
</div>

 

<!-- 子組件 child.html -->
<h2>我是子組件H2</h2>
<h3>我是子組件H3</h3>

一、 在子組件中,根據父組件來改變自身的樣式:

// 子組件樣式  child.component.scss
:host-context(.father) h2 {
  background: red;
}
:host-context(.other) h2 {
  background: orange;
}

結果:

 

 

 二、在父組件中,來修改子組件的樣式

// 父組件樣式  father.component.scss
::ng-deep dwb-child {
  h2 {
    color: beige;
  }
  h3 {
    color: blue;
  }
}

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM