javascript - 如何在 Angular 5 中从父组件继承子组件的 css 样式

标签 javascript html css angular angular5

我有一个父组件,其中有一个子组件。父组件有一些 css 类,子组件扩展了它们。我尝试使用 :host 查看文档,但似乎无法正常工作。

子组件:

<div class="table-row body">
<div class="table-cell body-content-outer-wrapper">
    <div class="body-content-inner-wrapper">
        <div class="body-content">
            <div>This is the scrollable content whose height is unknown</div>
            <div>This is the scrollable content whose height is unknown</div>
            <div>This is the scrollable content whose height is unknown</div>
        </div>
    </div>
</div>

父组件:

         <div class="table container">
                 <div class="table-row header">
                        <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">Simple collapsible</button>
                        <div id="demo" class="collapse">
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
                        </div>
                  </div>
            <app-child-component></app-child-component>
                </div>

父组件CSS:

  :host  .table {
    display: table;
  }

  :host .table-row {
    display: table-row;
  }

  :host .table-cell {
    display: table-cell;
  }

  :host .container {
   width: 400px;
   height: 100vh;
  }

  :host  .header {
    background: cyan;
 }

   :host .body {
    background: yellow;
    height: 100%;
 }

  :host .body-content-outer-wrapper {
    height: 100%;
 }

 .body-content-inner-wrapper {
    height: 100%;
   position: relative;
   overflow: auto;
}

 .body-content {
    position: absolute;
   top: 0;
   bottom: 0;
   left: 0;
   right: 0;
}

即使我将 css 类放在子组件中,布局也会因为存在子组件模板而中断。所以主要问题是子组件从父组件继承扩展 css 的正确方法是什么?

最佳答案

为什么要让事情变得复杂?

理想的做法是在子组件中也添加父 css 文件的引用。

 @Component({
    selector: 'child-app',
    templateUrl: `./child.component.html`,
    styleUrls:['./parent/parent.component.css', './child.component.css']
})
export class ChildComponent { }

关于javascript - 如何在 Angular 5 中从父组件继承子组件的 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516005/

相关文章:

html - 我无法摆脱的分区中链接上方和下方的额外空间?

javascript - 删除 div 部分下方的空白

android - Cordova android 滚动/性能问题

html - 如何在绝对定位元素中设置相对定位元素的高度?

php - 检测实时服务器端更改(长轮询、短轮询或 Web 套接字)

javascript - 如何在 fabric.js 的 Canvas 上触发自定义事件?

javascript - 验证正则表达式

html - 将图像(在网格中)放置在 div 旁边?

html - 在响应式方形 div 中垂直居中图像

javascript - 使用 leaflet-pip 绘制多边形中的点