简体   繁体   English

Angular - 继承父样式的子组件(不透明度)

[英]Angular - Child components inheriting parent styles (opacity)

I'm wondering if there's a way to stop Angular Child components inheriting the parents styles, mainly opacity.我想知道是否有办法阻止 Angular Child 组件继承父样式,主要是不透明度。

I have a form wrapper that sits on top of the page like so:我有一个位于页面顶部的表单包装器,如下所示:

import {Component, EventEmitter, Output} from "@angular/core";

@Component({
  selector: 'new-student-form-wrapper',
  templateUrl: './new-student-form-wrapper.component.html',
  styleUrls: ['./new-student-form-wrapper.component.scss']
})
export class NewStudentFormWrapperComponent {

  @Output() public onNewStudentFormClose: EventEmitter<void> = new EventEmitter<void>();

  public newStudentFormCloseClicked(): void {
    this.onNewStudentFormClose.emit();
  }

}

::ng-deep {
  .student-details-form {
    opacity: 0;
  }
}

.new-student-form-wrapper {
  z-index: 50;
  height: 100vh;
  width: 100vw;
  position: absolute;
  background-color: white;
  opacity: 0.8;

  .c-pointer {
    cursor: pointer;
  }
}

<div class="new-student-form-wrapper p-4">
  <mdb-icon class="c-pointer d-flex close-icon" (click)="newStudentFormCloseClicked()" fas icon="times"></mdb-icon>
  <new-student-form></new-student-form>
</div>

With a child component that will actually contain the form behaviour:使用将实际包含表单行为的子组件:

import {Component} from "@angular/core";

@Component({
  selector: 'new-student-form',
  templateUrl: './new-student-form.component.html',
  styleUrls: ['./new-student-form.component.scss']
})
export class NewStudentFormComponent {

}

.student-details-form {
  opacity: 1;
  background-color: purple; /** Just to test opacity*/
}

<div class="student-details-form md-form input-group d-flex flex-column">
  <div class="form-header">
    <h1>Hello</h1>
  </div>
</div>

However, despite actually explicitly declaring that new-student-form.component to have an opacity of 0, it is still picking up the container's 0.8 opacity rule.然而,尽管实际上明确声明 new-student-form.component 的不透明度为 0,它仍然采用容器的 0.8 不透明度规则。

在此处输入图片说明

I'm definitely using ng-deep wrong, what is the best way to remove the opacity for the child?我肯定使用 ng-deep 错误,去除孩子不透明度的最佳方法是什么? Is there an encapsulation rule I can use to stop the styles leaking into another component?是否有封装规则可以用来阻止样式泄漏到另一个组件中?

Opacity applies to the element as a whole, including its contents, even though the value is not inherited by child elements.不透明度适用于整个元素,包括其内容,即使该值不是由子元素继承的。 Thus, the element and its children all have the same opacity relative to the element's background, even if they have different opacities relative to one another.因此,元素及其子元素相对于元素的背景都具有相同的不透明度,即使它们相对于彼此具有不同的不透明度。

Not entirely sure if it will provide you with result you expect, but to define a transparent background, you can use the rgba on the background:不完全确定它是否会为您提供您期望的结果,但要定义透明背景,您可以在背景上使用rgba

.new-student-form-wrapper {
  z-index: 50;
  height: 100vh;
  width: 100vw;
  position: absolute;
  background-color: rgba(255, 255, 255, 0.8);
}

Because it looks like you are using scss, you can also use the transparentize utility function:因为看起来您正在使用 scss,所以您还可以使用transparentize实用程序功能:

background-color: transparentize(white, 0.2)

This way the form-wrapper is still a bit transparent, but any child content will be clearly visible这样表单包装器仍然有点透明,但任何子内容都将清晰可见


On a sidenote, that's indeed not the right way to use ::ng-deep .顺便说一句,这确实不是使用::ng-deep的正确方法。 Although it's one way to use it.虽然这是使用它的一种方式。 How you wrote it now will target any element in your document with the class .student-details-form .您现在的编写方式将针对您的文档中的任何元素使用.student-details-form To only target descendants of the current component, you need to add the :host selector:要仅针对当前组件的后代,您需要添加:host选择器:

:host ::ng-deep {
  .student-details-form {
    // style here
  }
}

To explain ::ng-deep a bit better, I'll just use some examples.为了更好地解释::ng-deep ,我将仅使用一些示例。 Basically what ::ng-deep does is that it does not add the _ngcontent-xxx-xxxx attribute selector to the emitted css.基本上::ng-deep所做的是它不会将_ngcontent-xxx-xxxx属性选择器添加到发出的 css 中。 (This happens when you use ViewEncapsulation.Emulated and thus only does something in a component stylesheet). (当您使用ViewEncapsulation.Emulated时会发生这种情况,因此只在组件样式表中执行某些操作)。

Soo, if you have the following in your component scss: Soo,如果您的组件 scss 中有以下内容:

::ng-deep {
  .form0 {
    opacity: 0;
  }
}

:host ::ng-deep {
  .form1 {
    opacity: 0.1
  }
}

:host {
  .form2 {
    opacity: 0.2;
  }
}

.form3 {
  opacity: 0.3;
}

.form-wrapper ::ng-deep {
  form4 {
    opacity: 0.4;
  }
}

This will result in something like this:这将导致如下结果:

.form0 {
  opacity: 0;
}

[_nghost-xxx-xxxx] .form1 {
  opacity: 0.1;
}

[_nghost-xxx-xxxx] .form2[_ngcontent-xxx-xxxx] {
  opacity: 0.2;
}

[_ngcontent-xxx-xxxx].form3 {
  opacity: 0.3;
}

.form-wrapper[_ngcontent-xxx-xxxx] .form4 {
  opacity: 0.4;
}

As you can see, if you use ::ng-deep on the root it is css without (emulated) encapsulation.如您所见,如果您在根上使用::ng-deep ,则它是没有(模拟)封装的 css。 Encapsulation is emulated through the host and content attribute selectors通过主机和内容属性选择器模拟封装

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

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