简体   繁体   English

angular:如何从本地组件访问应用程序根目录中的类名?

[英]angular: how can we access the class name in the app-root from local component?

I have three main pages and multiple child pages with each inherit its style from parent css. 我有三个主页和多个子页面,每个子页面都从父CSS继承其样式。

country.routes.ts 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 class is inserted to the a div just below the app-root like this 像这样,将CSS类插入到app-root下的div中

index.html (just rough html representation) 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>

in the main.scss (global style) 在main.scss中(全局样式)

country {
  background-color:red;
}
country-city {
  background-color:blue;
}
.country-fact {
  background-color: purple;
}

Because this is angular, therefore I want to exploit the view encapsulation and declare the style in the component declarator 因为这是有角度的,所以我想利用视图封装并在组件声明器中声明样式

country.component.ts country.component.ts

@component({ @零件({

 ...
 styles: ['
  :host-context(.country) main{
    background-color:red;
 ','
   :host-context(.country) header{
     display:none;
    }
 '],

country-city.component.ts 国家city.component.ts

@component({ @零件({

 ...
 styles: ['
  :host-context(.country-city) main{
    background-color:blue;
 '],

country-fact.component.ts 国家fact.component.ts

@component({ @零件({

 ...
 styles: ['
  :host-context(.country) main{
    background-color:purple;
 '],

From reading and tutorials: 通过阅读和教程:

The tutorial I read, so I change/edit parent's css by using the ':host' or ':host-context(.selector) .change-selector , but it doesn't say I can use the :host-context()` at the body or the app-root level. 我阅读了本教程 ,因此我通过使用':host'或':host-context(.selector).change-selector来更改/编辑父级的CSS , but it doesn't say I can use the :host-context()在主体或应用程序根目录级别上。

I tried nested :host or `:host{ :host-context()}, both were failed. 我尝试了嵌套:host或`:host {:host-context()},但是都失败了。

In the https://blog.angular-university.io/angular-host-context/ , it says that it is possible to select at the body level with the puseudo class but it does not work. https://blog.angular-university.io/angular-host-context/中 ,它说可以在主体级别使用puseudo类进行选择,但它不起作用。 I read some other article which I can't find the link, it says the :host-context is similar to :host with a selector, then I can only select my parent's class and not at the body level. 我读了一些其他文章,但找不到链接,它说:host-context类似于:host ,带有选择器,然后我只能选择父级的类,而不能在主体级选择。

The problem is that the main element is not part of the component you are applying the style to 问题是main元素不是您要向其应用样式的组件的一部分

From reading the official documentation for host-context ( https://angular.io/guide/component-styles#host-context ), you can only apply CSS styles to the elements INSIDE the component, based on some conditions outside of the component 通过阅读host-context的官方文档( https://angular.io/guide/component-styles#host-context ),您只能根据组件外部的某些条件将CSS样式应用于组件内部的元素

The following example applies a background-color style to all elements inside the component, only if some ancestor element has the CSS class theme-light. 以下示例仅在某些祖先元素具有CSS类theme-light的情况下,将背景颜色样式应用于组件的所有元素。

:host-context(.theme-light) h2 {
  background-color: #eef;
}

So to achieve what you are trying to do, you need to set these styles inside the component that has the main tag, or have the main tag inside your country components, or change the rules to target an element inside the country components (not the main tag) 因此,要实现您要执行的操作,您需要在具有main标签的组件内设置这些样式,或者在您的国家/地区组件内具有main标签,或者更改规则以在国家/地区组件内定位某个元素(而不是main标签)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM