css - 在 angular6 组件中使用动态 css

标签 css angular components modularity

我正在使用两个不同的组件 A:sea.component.ts 和 B:sun.component.ts 都在这些子组件中调用一个子组件,如果被 sea 或 sun 组件调用,我必须加载不同的 css。可以将 css url 作为输入传递给子组件:

 <sun_parent>
     <child [css]="sun_css"></child>
 </parent>

 <sea_parent>
     <child [css]="sun_css"></child>
 </sea_parent>

最佳答案

也许有办法,但这里有一个建议:你的两个组件调用同一个子组件,所以你可以通过在父 div 上使用 id 来利用它,如下所示:

这是一个例子:

https://stackblitz.com/edit/reuseable-component

<parent id="iAmSeaParent">
    <child></child>
</parent>

<parent id="iAmSunParent">
    <child></child>
</parent>

在你的 style.css 中

#iAmSeaParent childClassOrIdOrTag {
    background: yellow;
}

#iAmSunParent childClassOrIdOrTag {
    background: yellow;
}

关于css - 在 angular6 组件中使用动态 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51506271/

相关文章:

javascript - 如何拉伸(stretch)大于其自然尺寸的 img 标签中的图像?

angular - 如何正确返回 void Observable?

html - 根据屏幕分辨率更改 Material Design 响应按钮的内容

Angular 4在重新路由到它时调用组件方法

angular - 如何在 Angular2 中使用另一个组件中的变量

c# - 使用父级从组件 Blazor 调用方法

html - 我在文本区域、按钮及其布局方面遇到问题

php - 浏览的文件名未显示在文本框中

javascript - 为什么将动态内容添加到外部容器时内部元素宽度不会增加

typescript - TS2307 : Cannot find module 'ionic/ionic'