這個解決方案很好,但它更適用於任何常見樣式,這些樣式應該適用於所有組件。例如,CSS網格的樣式。 爲了使它更angularish你可以應用組件組封裝爲你無:
`@Component({
selector: 'my-app',
template: ` `,
styleUrls: ["shared.style.css"],
encapsulation: ViewEncapsulation.None
}) export class App {}`
Demo could be found here (plunker)
注:樣式,通過這個方式(只是添加樣式標籤,或與非封裝)包括將影響您網頁上的所有元素。有時它是我們真正想要的(同意爲洞項目使用任何css框架)。但是,如果只想在少數組件之間共享樣式 - 那可能不是最好的方式。
Summary:
(+) easy to use
(-) no encapsulation
2.我喜歡這個解決方案,因爲它是非常可以理解的,具有可預測的行爲。 但它有一個問題:
它會在您每次使用共享樣式時添加樣式標籤。 這可能是一個問題,如果你有很大的風格文件,或許多使用它的元素。

@Component({
selector: 'first',
template: `<h2> <ng-content> </ng-content> </h2>`,
styleUrls: ["shared.style.css"]
})
export class FirstComponent {}
Demo could be found here (plunker)
Summary:
(+) easy to use
(+) encapsulation
(-) duplicates styles for every usage
有可以使用多一個選擇。 只需創建一個組件,它將爲其子組件提供共享樣式。
` <styles-container>
<first> first comp </first>
</styles-container>
<styles-container>
<second> second comp </second>
</styles-container>`
在這種情況下,你將不得不使用/深/在你的風格,使可用的樣式子組件:
:host /deep/ h2 {
color: red;
}
我還值得一提的是不要忘記使用:讓主人款式可用只有爲子元素。如果你忽略它,你將會獲得更多的全球風格。
Demo could be found here (plunker)
Summary:
(-) you have to create container and it in templates
(+) encapsulation
(+) no duplicated styles
注:風格封裝是很酷的功能。但你也應該記住,沒有辦法限制你的深度風格。所以如果你使用了深色的款式,那麼它對所有的孩子都是絕對可用的,所以你也要小心使用它。