css - 如何将 css 属性传播到内部 Angular 组件和子组件?

标签 css angular

我有一个带有自定义 HTML 元素的 Angular 2 应用程序,如下所示:

<custom1>
 <div id="div1">
  <div id="div2"></div>
  <custom2>
   <div id="div3"></div>
  </custom2>
 </div>
</custom1>

在组件 custom1 中我有这个 css:

div {
 border-style: solid;
}

此样式适用于元素 div1div2,但不会传播到元素 div3

如何在不为 custom2 编写特定 CSS 的情况下将 custom1 的 CSS 属性传播到 custom2 中?

如果相关:custom1 包含在 Bootstrap 网格容器中。

最佳答案

您可以设置 View Encapsulation在您的 custom1 组件上设置为 None:

@Component({
    templateUrl: './custom1.component.html' ,
    styleUrls: ['./custom1.component.css'],
    encapsulation: ViewEncapsulation.None,
})

我提供的链接中的片段:

Component CSS styles are encapsulated into the component's view and don't affect the rest of the application.

To control how this encapsulation happens on a per component basis, you can set the view encapsulation mode in the component metadata. Choose from the following modes:

Native view encapsulation uses the browser's native shadow DOM implementation (see Shadow DOM on the MDN site) to attach a shadow DOM to the component's host element, and then puts the component view inside that shadow DOM. The component's styles are included within the shadow DOM.

Emulated view encapsulation (the default) emulates the behavior of shadow DOM by preprocessing (and renaming) the CSS code to effectively scope the CSS to the component's view. For details, see Appendix 1.

None means that Angular does no view encapsulation. Angular adds the CSS to the global styles. The scoping rules, isolations, and protections discussed earlier don't apply. This is essentially the same as pasting the component's styles into the HTML.

关于css - 如何将 css 属性传播到内部 Angular 组件和子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813795/

相关文章:

javascript - 单击 Angular 删除按钮时页面未刷新

javascript - Angular2 输出/发射()不工作

angular - 错误 : Uncaught (in promise): Response with status: 404 Not Found for URL when upgrading to Angular-CLI + Angular 4

php - Html - 更改 slider 的宽度和高度

php - 链接在 Wordpress 菜单中不可点击,但可以在新选项卡中打开

css - 如何使用 bootstrap3 在 ul 中对齐社交媒体图标

netbeans - 将 Angular2 指令添加到 Netbeans IDE

css - LESS CSS 命名约定 - 悬停时的子选择器

html - 媒体查询中关于 em 的困惑

angular - 如何订阅 Angular 2 中的变量?