css - 为什么是:host-context is required when :host can suffice the needs in Angular

标签 css angular

据我了解,:host-context 用于根据父级选择器应用样式。

让我们考虑如下规则:

:host-context(.red-theme) { 背景颜色:红色; }

同样可以使用 :host 选择器编写如下:

.red-theme :host { 背景颜色:红色; }

那么明确要求 host-context 是什么?

最佳答案

如果您想为组件自定义 HTML 元素本身设置样式,请使用 :host

:host-context 用于当您还希望让组件应用样式并考虑呈现组件的上下文时使用。

因此,例如,您可以这样做:(使用 host-context)

<div class="red-theme">
    <app-component></app-component>
</div>

哪里 app-component

<button class="btn btn-theme">Button</button>

并且定义了组件样式:

:host-context(.red-theme) .btn-theme {
    background: red;
 }

如果您想在您的 Web 应用程序上有多个替代主题,这很有用。

关于css - 为什么是:host-context is required when :host can suffice the needs in Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56777350/

相关文章:

javascript - 避免为动态内容制作一堆不同的弹出窗口

html - 文本突出显示在顶部被切断

php - 有序/无序列表标签 <ol><ul> 不显示列表元素符号

jquery - 我的 jquery sortable 没有排序

css - Angular Material Style 完成的步骤

Angular CLI 错误 : The serve command requires to be run in an Angular project, 但找不到项目定义

html - 如何停止图层之间的混合?

Angular NgRx 效果,如何传递参数?

html - 如何导航到 Angular 6 中的其他页面?

javascript - 在 Chrome 中选择 "Open Link in New Tab"选项时,应用程序路由重定向到 Angular 应用程序中的登录