【发布时间】:2018-09-10 08:08:15
【问题描述】:
我有三个主要页面和多个子页面,每个页面都从父 css 继承其样式。
country.routes.ts
...
path: '',
children: [
{
path: '/country',
component: CountryComponent,
data: {
cssClass: 'country',
},
},
{
path: ':countryId/city',
component: CityComponent ,
data: {
cssClass: 'country-city',
},
},
{
path: ':countryId/fact',
component: FactComponent,
data: {
cssClass: 'country-fact',
},
},
...
CSS 类像这样插入到 app-root 正下方的 div 中
index.html(只是粗略的 html 表示)
<app-root>
<div clas="country/country-fact/country-city">
<header></header>
<div>
<route-outlet>
<main>
<!-- content -->
</main>
</route-outlet>
</div>
<footer></footer>
</div>
</app-root>
在 main.scss(全局样式)中
country {
background-color:red;
}
country-city {
background-color:blue;
}
.country-fact {
background-color: purple;
}
因为这是有角度的,所以我想利用视图封装并在组件声明器中声明样式
country.component.ts
@组件({
...
styles: ['
:host-context(.country) main{
background-color:red;
','
:host-context(.country) header{
display:none;
}
'],
country-city.component.ts
@组件({
...
styles: ['
:host-context(.country-city) main{
background-color:blue;
'],
country-fact.component.ts
@组件({
...
styles: ['
:host-context(.country) main{
background-color:purple;
'],
来自阅读和教程:
我读到了tutorial,所以我在正文或应用程序根级别。
我尝试嵌套:host 或`:host{ :host-context()},都失败了。
在https://blog.angular-university.io/angular-host-context/ 中,它说可以使用 puseudo 类在正文级别进行选择,但它不起作用。我读了一些我找不到链接的其他文章,它说:host-context 类似于带有选择器的:host,然后我只能选择我父母的班级而不是正文级别。
【问题讨论】:
-
您的组件在层次结构中的位置?在
<main>??你能在 stackblitz.com 上创建一个小例子吗? -
组件在下定义
-
您在下面看到我的答案吗?
标签: html css angular typescript