简体   繁体   English

如何从父组件的 CSS 文件中设置子组件的样式?

[英]How to style child components from parent component's CSS file?

I've got a parent component:我有一个父组件:

<parent></parent>

And I want to populate this group with child components:我想用子组件填充这个组:

<parent>
  <child></child>
  <child></child>
  <child></child>
</parent>

Parent template:父模板:

<div class="parent">
  <!-- Children goes here -->
  <ng-content></ng-content>
</div>

Child template:子模板:

<div class="child">Test</div>

Since parent and child are two separate components, their styles are locked to their own scope.由于parentchild是两个独立的组件,因此它们的 styles 被锁定到它们自己的 scope。

In my parent component I tried doing:在我的父组件中,我尝试做:

.parent .child {
  // Styles for child
}

But the .child styles are not getting applied to the child components.但是.child styles 没有应用于child组件。

I tried using styleUrls to include the parent 's stylesheet into child component to solve the scope issue:我尝试使用styleUrlsparent的样式表包含到child组件中以解决 scope 问题:

// child.component.ts
styleUrls: [
  './parent.component.css',
  './child.component.css',
]

But that didn't help, also tried the other way by fetching the child stylesheet into parent but that didn't help either.但这并没有帮助,还尝试了另一种方式,将child样式表获取到parent中,但这也无济于事。

So how do you style child components that are included into a parent component?那么如何设置包含在父组件中的子组件的样式呢?

Update - Newest Way更新 - 最新方式

Don't do it, if you can avoid it.如果可以避免,请不要这样做。 As Devon Sans points out in the comments: This feature will most likely be deprecated.正如 Devon Sans 在评论中指出的那样:此功能很可能会被弃用。

#Update - Newer Way From Angular 4.3.0 , all piercing css combinators were deprecated. #Update - 从Angular 4.3.0 开始,所有穿孔 css 组合器都被弃用了。 Angular team introduced a new combinator ::ng-deep (still it is experimental and not the full and final way) as shown below, Angular 团队引入了一个新的组合器::ng-deep (它仍然是实验性的,而不是完整的和最终的方式) ,如下所示,

DEMO : https://plnkr.co/edit/RBJIszu14o4svHLQt563?p=preview演示: https : //plnkr.co/edit/RBJIszu14o4svHLQt563? p =preview

styles: [
    `
     :host { color: red; }
     
     :host ::ng-deep parent {
       color:blue;
     }
     :host ::ng-deep child{
       color:orange;
     }
     :host ::ng-deep child.class1 {
       color:yellow;
     }
     :host ::ng-deep child.class2{
       color:pink;
     }
    `
],



template: `
      Angular2                                //red
      <parent>                                //blue
          <child></child>                     //orange
          <child class="class1"></child>      //yellow
          <child class="class2"></child>      //pink
      </parent>      
    `

# Old way #老方法

You can use encapsulation mode and/or piercing CSS combinators >>>, /deep/ and ::shadow您可以使用encapsulation mode和/或piercing CSS combinators >>>, /deep/ and ::shadow

working example : http://plnkr.co/edit/1RBDGQ?p=preview工作示例: http : //plnkr.co/edit/1RBDGQ?p=preview

 styles: [ ` :host { color: red; } :host >>> parent { color:blue; } :host >>> child{ color:orange; } :host >>> child.class1 { color:yellow; } :host >>> child.class2{ color:pink; } ` ], template: ` Angular2 //red <parent> //blue <child></child> //orange <child class="class1"></child> //yellow <child class="class2"></child> //pink </parent> `

You should NOT use ::ng-deep , it is deprecated.您不应该使用::ng-deep ,它已被弃用。 In Angular, the proper way to change the style of children's component from the parent is to use encapsulation (read the warning below to understand the implications):在 Angular 中,从父级更改子组件样式的正确方法是使用encapsulation (阅读下面的警告以了解其含义):

import { ViewEncapsulation } from '@angular/core';

@Component({
    ....
    encapsulation: ViewEncapsulation.None
})

And then, you will be able to modify the css form your component without a need from ::ng-deep然后,您将能够在不需要 ::ng-deep 的情况下修改组件的 css 表单

.mat-sort-header-container {
  display: flex;
  justify-content: center;
}

WARNING: Doing this will make all css rules you write for this component to be global.警告:这样做将使您为此组件编写的所有 css 规则成为全局规则。

In order to limit the scope of your css to this component and his child only, add a css class to the top tag of your component and put your css "inside" this tag:为了将你的 css 的范围限制在这个组件和他的子组件,添加一个 css 类到你的组件的顶部标签,并将你的 css 放在这个标签的“内部”:

template:
    <div class='my-component'>
      <child-component class="first">First</child>
    </div>,

Scss file: .scss 文件:

.my-component {
  // All your css goes in there in order not to be global
}

UPDATE 3:更新 3:

::ng-deep is also deprecated which means you should not do this at all anymore. ::ng-deep也已弃用,这意味着您根本不应再这样做了。 It is unclear how this affects things where you need to override styles in child components from a parent component.目前尚不清楚这如何影响您需要从父组件覆盖子组件中的样式的事情。 To me it seems odd if this gets removed completely because how would this affect things as libraries where you need to override styles in a library component?对我来说,如果这被完全删除似乎很奇怪,因为这将如何影响作为需要覆盖库组件中样式的库的东西?

Comment if you have any insight in this.如果您对此有任何见解,请发表评论。

UPDATE 2:更新 2:

Since /deep/ and all other shadow piercing selectors are now deprecated.由于/deep/和所有其他阴影穿透选择器现在已弃用。 Angular dropped ::ng-deep which should be used instead for a broader compatibility. Angular 删除了::ng-deep ,应该使用它来实现更广泛的兼容性。

UPDATE:更新:

If using Angular-CLI you need to use /deep/ instead of >>> or else it will not work.如果使用 Angular-CLI,您需要使用/deep/而不是>>>否则它将无法工作。

ORIGINAL:原文:

After going to Angular2's Github page and doing a random search for "style" I found this question: Angular 2 - innerHTML styling在转到 Angular2 的 Github 页面并随机搜索“样式”后,我发现了这个问题: Angular 2 - innerHTML 样式

Which said to use something that was added in 2.0.0-beta.10 , the >>> and ::shadow selectors.据说使用了在2.0.0-beta.10添加的东西, >>>::shadow选择器。

(>>>) (and the equivalent/deep/) and ::shadow were added in 2.0.0-beta.10. (>>>)(以及等效的/deep/)和 ::shadow 是在 2.0.0-beta.10 中添加的。 They are similar to the shadow DOM CSS combinators (which are deprecated) and only work with encapsulation: ViewEncapsulation.Emulated which is the default in Angular2.它们类似于 shadow DOM CSS 组合器(已弃用)并且仅适用于封装:ViewEncapsulation.Emulated,这是 Angular2 中的默认值。 They probably also work with ViewEncapsulation.None but are then only ignored because they are not necessary.它们可能也与 ViewEncapsulation.None 一起使用,但随后只会被忽略,因为它们不是必需的。 These combinators are only an intermediate solution until more advanced features for cross-component styling is supported.在支持跨组件样式的更高级功能之前,这些组合器只是一个中间解决方案。

So simply doing:所以简单地做:

:host >>> .child {}

In parent 's stylesheet file solved the issue.parent的样式表文件中解决了这个问题。 Please note, as stated in the quote above, this solution is only intermediate until more advanced cross-component styling is supported.请注意,如上面引用所述,在支持更高级的跨组件样式之前,此解决方案只是中间的。

Sadly it appears that the /deep/ selector is deprecated (at least in Chrome) https://www.chromestatus.com/features/6750456638341120可悲的是,似乎 /deep/ 选择器已被弃用(至少在 Chrome 中) https://www.chromestatus.com/features/6750456638341120

In short it appears there is (currently) no long term solution other than to somehow get your child component to style things dynamically.简而言之,除了以某种方式让您的子组件动态设置样式之外,似乎(目前)没有长期解决方案。

You could pass a style object to your child and have it applied via:您可以将样式对象传递给您的孩子并通过以下方式应用它:
<div [attr.style]="styleobject">

Or if you have a specific style you can use something like:或者,如果您有特定的样式,则可以使用以下内容:
<div [style.background-color]="colorvar">

More discussion related to this: https://github.com/angular/angular/issues/6511与此相关的更多讨论: https : //github.com/angular/angular/issues/6511

有同样的问题,所以如果你将 angular2-cli 与 scss/sass 一起使用,请使用“/deep/”而不是“>>>”,最后一个选择器尚不支持(但与 css 一起使用时效果很好)。

If you want to be more targeted to the actual child component than you should do the follow.如果您想更加针对实际的子组件而不是您应该执行以下操作。 This way, if other child components share the same class name, they won't be affected.这样,如果其他子组件共享相同的类名,它们就不会受到影响。

Plunker: https://plnkr.co/edit/ooBRp3ROk6fbWPuToytO?p=preview Plunker: https ://plnkr.co/edit/ooBRp3ROk6fbWPuToytO ? p = preview

For example:例如:

import {Component, NgModule } from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>I'm the host parent</h2>
      <child-component class="target1"></child-component><br/>
      <child-component class="target2"></child-component><br/>
      <child-component class="target3"></child-component><br/>
      <child-component class="target4"></child-component><br/>
      <child-component></child-component><br/>
    </div>
  `,
  styles: [`

  /deep/ child-component.target1 .child-box {
      color: red !important; 
      border: 10px solid red !important;
  }  

  /deep/ child-component.target2 .child-box {
      color: purple !important; 
      border: 10px solid purple !important;
  }  

  /deep/ child-component.target3 .child-box {
      color: orange !important; 
      border: 10px solid orange !important;
  }  

  /* this won't work because the target component is spelled incorrectly */
  /deep/ xxxxchild-component.target4 .child-box {
      color: orange !important; 
      border: 10px solid orange !important;
  }  

  /* this will affect any component that has a class name called .child-box */
  /deep/ .child-box {
      color: blue !important; 
      border: 10px solid blue !important;
  }  


  `]
})
export class App {
}

@Component({
  selector: 'child-component',
  template: `
    <div class="child-box">
      Child: This is some text in a box
    </div>
  `,
  styles: [`
    .child-box {
      color: green;    
      border: 1px solid green;
    }
  `]
})
export class ChildComponent {
}


@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, ChildComponent ],
  bootstrap: [ App ]
})
export class AppModule {}

Hope this helps!希望这有帮助!

codematrix代码矩阵

You should not write CSS rules for a child component elements in a parent component, since an Angular component is a self-contained entity which should explicitly declare what is available for the outside world.你不应该为父组件中的子组件元素编写 CSS 规则,因为 Angular 组件是一个独立的实体,它应该明确声明外部世界可用的内容。 If child layout changes in the future, your styles for that child component elements scattered across other components' SCSS files could easily break, thus making your styling very fragile.如果将来子布局发生变化,那么分散在其他组件的 SCSS 文件中的子组件元素的样式很容易被破坏,从而使您的样式变得非常脆弱。 That's what ViewEncapsulation is for in the case of CSS.在 CSS 的情况下,这就是ViewEncapsulation的用途。 Otherwise, it would be the same if you could assign values to private fields of some class from any other class in Object Oriented Programming.否则,如果您可以将值分配给面向对象编程中任何其他类的某个类的私有字段,那将是相同的。

Therefore, what you should do is to define a set of classes you could apply to the child host element and implement how the child responds to them.因此,您应该做的是定义一组可以应用于子宿主元素的类,并实现子元素如何响应它们。

Technically, it could be done as follows:从技术上讲,可以按如下方式完成:

// child.component.html:
<span class="label-1"></span>

// child.component.scss:
:host.child-color-black {
    .label-1 {
        color: black;
    }
}

:host.child-color-blue {
    .label-1 {
        color: blue ;
    }
}

// parent.component.html:
<child class="child-color-black"></child>
<child class="child-color-blue"></child>

In other words, you use :host pseudo-selector provided by Angular + set of CSS classes to define possible child styles in child component itself.换句话说,您使用 Angular 提供的:host伪选择器 + CSS 类集来定义子组件本身中可能的子样式。 You then have the ability to trigger those styles from outside by applying pre-defined classes to the <child> host element.然后,您可以通过将预定义的类应用于<child>宿主元素来从外部触发这些样式。

Actually there is one more option.其实还有一种选择。 Which is rather safe.这是比较安全的。 You can use ViewEncapsulation.None BUT put all your component styles into its tag (aka selector).您可以使用 ViewEncapsulation.None 但将所有组件样式放入其标记(又名选择器)中。 But anyway always prefer some global style plus encapsulated styles.但无论如何总是更喜欢一些全局样式加上封装样式。

Here is modified Denis Rybalka example:这是修改后的 Denis Rybalka 示例:

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'parent',
  styles: [`
    parent {
      .first {
        color:blue;
      }
      .second {
        color:red;
      }
    }
 `],
 template: `
    <div>
      <child class="first">First</child>
      <child class="second">Second</child>
    </div>`,
  encapsulation: ViewEncapsulation.None,
})
export class ParentComponent  {
  constructor() { }
}

There are a few options to achieve this in Angular:在 Angular 中有几个选项可以实现这一点:

1) You can use deep css selectors 1) 您可以使用深 css 选择器

:host >>> .childrens {
     color: red;
 }

2) You can also change view encapsulation it's set to Emulated as a default but can be easily changed to Native which uses Shadow DOM native browser implementation, in your case you just need to disable it 2) 您还可以更改视图封装,它默认设置为 Emulated,但可以轻松更改为使用 Shadow DOM 本机浏览器实现的 Native,在您的情况下,您只需要禁用它

For example:`例如:`

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'parent',
  styles: [`
    .first {
      color:blue;
    }
    .second {
      color:red;
    }
 `],
 template: `
    <div>
      <child class="first">First</child>
      <child class="second">Second</child>
    </div>`,
  encapsulation: ViewEncapsulation.None,
 })
 export class ParentComponent  {
   constructor() {

   }
 }

I find it a lot cleaner to pass an @INPUT variable if you have access to the child component code:如果您可以访问子组件代码,我发现传递 @INPUT 变量会更清晰

The idea is that the parent tells the child what its state of appearance should be, and the child decides how to display the state.这个想法是父母告诉孩子它的外观状态应该是什么,孩子决定如何显示状态。 It's a nice architecture这是一个不错的架构

SCSS Way: SCSS方式:

.active {
  ::ng-deep md-list-item {
    background-color: #eee;
  }
}

Better way: - use selected variable:更好的方法: - 使用selected变量:

<md-list>
    <a
            *ngFor="let convo of conversations"
            routerLink="/conversations/{{convo.id}}/messages"
            #rla="routerLinkActive"
            routerLinkActive="active">
        <app-conversation
                [selected]="rla.isActive"
                [convo]="convo"></app-conversation>
    </a>
</md-list>

As of today (Angular 9), Angular uses a Shadow DOM to display the components as custom HTML elements .截至今天(Angular 9),Angular 使用Shadow DOM将组件显示为自定义 HTML 元素 One elegant way to style those custom elements might be using custom CSS variables .为这些自定义元素设置样式的一种优雅方式可能是使用自定义 CSS 变量 Here is a generic example:这是一个通用示例:

 class ChildElement extends HTMLElement { constructor() { super(); var shadow = this.attachShadow({mode: 'open'}); var wrapper = document.createElement('div'); wrapper.setAttribute('class', 'wrapper'); // Create some CSS to apply to the shadow dom var style = document.createElement('style'); style.textContent = ` /* Here we define the default value for the variable --background-clr */ :host { --background-clr: green; } .wrapper { width: 100px; height: 100px; background-color: var(--background-clr); border: 1px solid red; } `; shadow.appendChild(style); shadow.appendChild(wrapper); } } // Define the new element customElements.define('child-element', ChildElement);
 /* CSS CODE */ /* This element is referred as :host from the point of view of the custom element. Commenting out this CSS will result in the background to be green, as defined in the custom element */ child-element { --background-clr: yellow; }
 <div> <child-element></child-element> </div>

As we can see from the above code, we create a custom element, just like Angular would do for us with every component, and then we override the variable responsible for the background color within the shadow root of the custom element, from the global scope.从上面的代码中我们可以看到,我们创建了一个自定义元素,就像 Angular 为我们对每个组件所做的那样,然后我们从全局范围覆盖自定义元素的阴影根中负责背景颜色的变量.

In an Angular app, this might be something like:在 Angular 应用程序中,这可能类似于:

parent.component.scss父组件.scss

child-element {
  --background-clr: yellow;
}

child-element.component.scss子元素.component.scss

:host {
  --background-clr: green;
}

.wrapper {
  width: 100px;
  height: 100px;
  background-color: var(--background-clr);
  border: 1px solid red;
}

Since /deep/, >>>, and ::ng-deep are all deprecated.由于 /deep/、>>> 和 ::ng-deep 都已弃用。 The best approach is to use the following in your child component styling最好的方法是在您的子组件样式中使用以下内容

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

This will look for the theme-light in any of the ancestors of your child component.这将在您的子组件的任何祖先中查找主题灯。 See docs here: https://angular.io/guide/component-styles#host-context请参阅此处的文档: https : //angular.io/guide/component-styles#host-context

The quick answer is you shouldn't be doing this, at all.快速回答是你根本不应该这样做。 It breaks component encapsulation and undermines the benefit you're getting from self-contained components.它破坏了组件封装并破坏了您从独立组件中获得的好处。 Consider passing a prop flag to the child component, it can then decide itself how to render differently or apply different CSS, if necessary.考虑将 prop 标志传递给子组件,然后它可以自行决定如何以不同方式呈现或应用不同的 CSS(如有必要)。

<parent>
  <child [foo]="bar"></child>
</parent>

Angular is deprecating all ways of affecting child styles from parents. Angular 正在弃用所有影响父母风格的方式。

https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep

i also had this problem and didnt wanted to use deprecated solution so i ended up with:我也有这个问题,不想使用已弃用的解决方案,所以我最终得到:

in parrent在父母

 <dynamic-table
  ContainerCustomStyle='width: 400px;'
  >
 </dynamic-Table>

child component子组件

@Input() ContainerCustomStyle: string;

in child in html div在 html div 中的孩子

 <div class="container mat-elevation-z8"
 [style]='GetStyle(ContainerCustomStyle)' >

and in code并在代码中

constructor(private sanitizer: DomSanitizer) {  }

  GetStyle(c) {
    if (isNullOrUndefined(c)) { return null; }
    return  this.sanitizer.bypassSecurityTrustStyle(c);
  }

works like expected and should not be deprecated ;)像预期的那样工作,不应该被弃用;)

For assigning an element's class in a child component you can simply use an @Input string in the child's component and use it as an expression inside the template.要在子组件中分配元素的类,您可以简单地在子组件中使用@Input字符串,并将其用作模板内的表达式。 Here is an example of something we did to change the icon and button type in a shared Bootstrap loading button component, without affecting how it was already used throughout the codebase:这是我们在共享 Bootstrap 加载按钮组件中更改图标和按钮类型的示例,而不影响它在整个代码库中的使用方式:

app-loading-button.component.html (child) app-loading-button.component.html (child)

<button class="btn {{additionalClasses}}">...</button>

app-loading-button.component.ts app-loading-button.component.ts

@Input() additionalClasses: string;

parent.html父.html

<app-loading-button additionalClasses="fa fa-download btn-secondary">...</app-loading-button>

What I prefer to achieve this is the following:我更喜欢实现以下目标:

use @Component to add css class to host element and set encapsulation to none.使用@Component将 css 类添加到宿主元素并将封装设置为无。 Then reference that class which was added to the host within the components style.css.scss This will allow us to declare styles which will only affect ourselves and our children within scope of our class.然后引用在组件style.css.scss添加到宿主的那个类。这将允许我们声明样式,这些样式只会影响我们类范围内的我们自己和我们的孩子。 fe

@Component({
  selector: 'my-component',
  templateUrl: './my-component.page.html',
  styleUrls: ['./my-component.page.scss'],
  host: {
    class: 'my-component-class'
  },
  encapsulation: ViewEncapsulation.None
})

in combination with the following css (my-component.page.scss)结合以下 css (my-component.page.scss)

// refer ourselves so we are allowed to overwrite children but not global styles
.my-component-class {
  // will effect direct h1 nodes within template and all h1 elements within child components of the 
  h1 {
    color: red;
  }
}
// without class "scope" will affect all h1 elements globally
h1 {
  color: blue;
}

As the internet updates I've come across a solution.随着互联网的更新,我遇到了一个解决方案。

First some caveats.首先是一些警告。

  1. Still don't do it.还是别做了。 To clarify, I wouldn't plan on child components allowing you to style them.澄清一下,我不会计划允许您设置样式的子组件。 SOC. SOC。 If you as the component designer want to allow this then all the more power to you.如果您作为组件设计者希望允许这样做,那么您将获得更多权力。
  2. If your child doesn't live in the shadow dom then this won't work for you.如果您的孩子不住在 shadow dom 中,那么这对您不起作用。
  3. If you have to support a browser that can't have a shadow dom then this also won't work for you.如果您必须支持不能有 shadow dom 的浏览器,那么这也不适用于您。

First, mark your child component's encapsulation as shadow so it renders in the actual shadow dom.首先,将子组件的封装标记为阴影,以便它在实际的阴影 dom 中呈现。 Second, add the part attribute to the element you wish to allow the parent to style.其次,将 part 属性添加到您希望允许父级设置样式的元素。 In your parent's component stylesheet you can use the ::part() method to access在您的父组件样式表中,您可以使用 ::part() 方法来访问

This is a solution with just vanilla css, nothing fancy, you don't even need !important .这是一个只有香草 css 的解决方案,没什么花哨的,你甚至不需要!important I assume you can't modify the child, otherwise the answer is even more simple, I put that answer at the end just in case.我假设你不能修改孩子,否则答案就更简单了,我把答案放在最后以防万一。

Overriding a child's CSS is sometimes needed when using a pre-made component from a library, and the devs have not provided any class input variables.在使用库中的预制组件时,有时需要覆盖孩子的 CSS,并且开发人员没有提供任何class输入变量。 ::ng-deep is deprecated and encapsulation: ViewEncapsulation.None turns all of your component's CSS global. ::ng-deep已弃用和encapsulation: ViewEncapsulation.None将所有组件的 CSS 变为全局。 So here is a simple solution that uses neither of those.所以这里有一个简单的解决方案,它不使用这两种方法。

The fact is, we do need a global style in order for the CSS to reach the child.事实上,为了让 CSS 到达孩子,我们确实需要一个全局样式。 So, we can just put the style in styles.css or we can create a new CSS file and add it to the styles array in angular.json . So, we can just put the style in styles.css or we can create a new CSS file and add it to the styles array in angular.json . The only issue is that we need a specific selector so as not to target other elements.唯一的问题是我们需要一个特定的选择器,以免针对其他元素。 That's a pretty easy solution - just add a unique class name to the html, I recommend using the parent component's name in the class name to ensure it is unique.这是一个非常简单的解决方案 - 只需在 html 中添加一个唯一的 class 名称,我建议在 class 名称中使用父组件的名称以确保它是唯一的。

Parent Component父组件

<child class="child-in-parent-component"></child>

Let's pretend we want to change the background-color of all the buttons within the child, we do need to achieve the correct specificity to make sure our styles take precedence.假设我们想要更改子元素中所有按钮的背景颜色,我们确实需要实现正确的特异性以确保我们的 styles 优先。 We can do this with !important next to all our properties, but a better way is to just repeat the class name until our selector is specific enough, may take a few tries.我们可以在所有属性旁边使用!important来做到这一点,但更好的方法是重复 class 名称,直到我们的选择器足够具体,可能需要几次尝试。 That way, someone else can override this css again if necessary.这样,如有必要,其他人可以再次覆盖此 css。

Global Styles File全局 Styles 文件

.child-in-parent-component.child-in-parent-component.child-in-parent-component
  button {
  background-color: red;
}

or quick and dirty with !important (not recommended)或使用!important快速而肮脏(不推荐)

.child-in-parent-component button {
  background-color: red !important;
}

If the child component can be modified如果子组件可以修改

Simply add an input variable to the component and make use of Angular's ngStyle directive.只需向组件添加一个输入变量并使用 Angular 的ngStyle指令。 You can add multiple variables to style multiple areas of your component.您可以添加多个变量来设置组件的多个区域的样式。

Child Component子组件

type klass = { [prop: string]: any } | null;

@Component({...})
export class ChildComponent {
  @Input() containerClass: klass = null;
  @Input() pClass: klass = null;
...
}
<div [ngStyle]="containerClass">
  <p [ngStyle]="pClass">What color will I be?</p>
</div>

Parent Component父组件

<child
  [containerClass]="{ padding: '20px', 'background-color': 'black' }"
  [pClass]="{ color: 'red' }"
>
</child>

This is the intended way to create a component with a dynamic style.这是创建具有动态样式的组件的预期方式。 Many pre-made components will have a similar input variable.许多预制组件将具有类似的输入变量。

this has worked for me这对我有用

in the parent component:在父组件中:

<child-component [styles]="{width: '160px', borderRadius: '16px'}" >
</child-component>

I propose an example to make it more clear, since angular.io/guide/component-styles states:我提出了一个更清楚的例子,因为angular.io/guide/component-styles声明:

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools.不推荐使用阴影穿透后代组合器,并且正在从主要浏览器和工具中删除支持。 As such we plan to drop support in Angular (for all 3 of /deep/, >>> and ::ng-deep).因此,我们计划放弃对 Angular 的支持(对于 /deep/、>>> 和 ::ng-deep 中的所有 3 个)。 Until then ::ng-deep should be preferred for a broader compatibility with the tools.在那之前 ::ng-deep 应该是首选,因为它与工具更广泛地兼容。

On app.component.scss , import your *.scss if needed.app.component.scss ,根据需要导入您的*.scss app.component.scss _colors.scss has some common color values: _colors.scss有一些常见的颜色值:

$button_ripple_red: #A41E34;
$button_ripple_white_text: #FFF;

Apply a rule to all components将规则应用于所有组件

All the buttons having btn-red class will be styled.所有具有btn-red类的按钮都将被设置样式。

@import `./theme/sass/_colors`;

// red background and white text
:host /deep/ button.red-btn {
    color: $button_ripple_white_text;
    background: $button_ripple_red;
}

Apply a rule to a single component将规则应用于单个组件

All the buttons having btn-red class on app-login component will be styled. app-login组件上所有具有btn-red类的按钮都将被设置样式。

@import `./theme/sass/_colors`;

/deep/ app-login button.red-btn {
    color: $button_ripple_white_text;
    background: $button_ripple_red;
}

I have solved it outside Angular.我已经在 Angular 之外解决了它。 I have defined a shared scss that I'm importing to my children.我定义了一个共享的 scss,我要导入我的孩子们。

shared.scss共享文件

%cell {
  color: #333333;
  background: #eee;
  font-size: 13px;
  font-weight: 600;
}

child.scss孩子.scss

@import 'styles.scss';
.cell {
  @extend %cell;
}

My proposed approach is a way how to solve the problem the OP has asked about.我提出的方法是一种如何解决 OP 提出的问题的方法。 As mentioned at multiple occasions, ::ng-deep, :ng-host will get depreciated and disabling encapsulation is just too much of a code leakage, in my view.正如在多个场合所提到的,::ng-deep、:ng-host 会贬值,在我看来,禁用封装是太多的代码泄漏。

let 'parent' be the class-name of parent and 'child' be the class-name of child让“parent”是父母的类名,“child”是孩子的类名

.parent .child{
//css definition for child inside parent components
} 

you can use this format to define CSS format to 'child' component inside the 'parent'您可以使用此格式将 CSS 格式定义为“父”内的“子”组件

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

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