【问题标题】:Why does my angular component ignore my css file?为什么我的角度组件会忽略我的 css 文件?
【发布时间】:2019-08-30 05:56:53
【问题描述】:

我有一个具有此文件结构的角度组件:

.
|____custom-d3-tree.component.css
|____custom-d3-tree.component.spec.ts
|____custom-d3-tree.component.ts
|____custom-d3-tree.module.ts
|____custom-d3-tree.service.spec.ts
|____custom-d3-tree.service.ts
|____tree.dendo.model.ts

文件tree.denco.model.ts 定义:

export class TreeModel {

它实现了一个 d3 树。树中的节点定义了foreignObject,其中包含一个订单列表。这是将 foreignObject 添加到矩形节点的代码:

    nodeEnter.append('foreignObject')
      .attr('x', function(d) { if(d.data.component_type) { return 5; } return 0; })
      .attr('y', function(d) { if(d.data.component_type) { return 5; } return 0; })
      .attr('width', this.rect_width)
      .attr('height', this.rect_height)
      .style('overflow', 'auto')
      .append('xhtml')
      .style("font", "10px 'Helvetica Neue'")
      .html(function (d) {
        if( d.data.component_type ) {
          return '<div style="width: ' + (d.width - 10) + 'px; height: '
            + (d.height - 10 ) + 'px;" class="node-text wordwrap">'
            + '<b>' +  d.data.name + '</b><br><br></div>';
        } else {
          let scrollList = '<nav><ul>';
          let startDate = "2019-04-05";
          for( let i=0; i < d.data.components.length; i++ ) {
            let url = environment.frontEndUrl + '/component-mapping-dynamic?node='
              + d.data.name + '&filter_key=' + d.data.components[i].filter_key;
            // scrollList += '<li style="list-style: ' + 'none' + '"><a href="'+ url + '">' + d.data.components[i].name + '</a></li>';
            scrollList += '<li><a href="'+ url + '">' + d.data.components[i].name + '</a></li>';
          }
          scrollList += '</ul></nav>';
          return '<div>' +  scrollList + '</div>';
        }
    });

我有一个 css 文件,custom-d3-tree.component.css,其中包含:

.d3-chart {
  width: 100%;
  min-height:600px;
}

.wordwrap {
  white-space: pre-wrap; /* CSS3 */
  white-space: -moz-pre-wrap; /* Firefox */
  white-space: -pre-wrap; /* Opera <7 */
  white-space: -o-pre-wrap; /* Opera 7 */
  word-wrap: break-word; /* IE */
}

div:hover {
  overflow-y: scroll;
}

ul {
  list-style-type: circle;
}
nav ul{height:1000px; width:18%; list-style-type: none;}

div {
  height: 100px;
  width: 50%;
  margin: 0 auto;
  overflow: hidden;
  overflow-y: scroll;
}

/* nav ul{height:100px; width:18%; list-style-type: none;} */
/* nav ul{overflow:hidden; overflow-y:scroll;} */

这是我的代码运行时树的样子:

如您所见,我文件中的大部分(如果不是所有)css 元素都被忽略了,我不明白为什么。

更新:这是我的custom-d3-tree.component.ts 文件:

import { Component, OnInit, OnChanges, ViewChild, ElementRef,
   Input, Output, EventEmitter} from '@angular/core';

import { AngularD3TreeLibService } from './custom-d3-tree.service';
@Component({
  selector: 'custom-angular-d3-tree-lib',
  template: `
    <div class="d3-chart" #chart></div>
  `,
  styleUrls: ['./custom-d3-tree.component.css']
})
export class AngularD3TreeLibComponent implements OnInit, OnChanges {
  @ViewChild('chart') private chartContainer: ElementRef;
  @Input() treeData: any = [];
  @Output() onNodeChanged: EventEmitter<any>= new EventEmitter();
  @Output() onNodeSelected: EventEmitter<any>= new EventEmitter();

  constructor( private treeService: AngularD3TreeLibService ) {
    treeService.setNodeChangedListener((node)=>{
      this.onNodeChanged.emit(node);
    });
    treeService.setNodeSelectedListener((node)=>{
      this.onNodeSelected.emit(node);
    });
  }

  ngOnInit() {}
  ngOnChanges(changes: any) {
    this.seedTree();
  }

  seedTree(){
    if(!!this.treeData){
      this.treeService.createChart(this.chartContainer, this.treeData);
      this.treeService.update();
    }
  }
}

【问题讨论】:

  • 你是如何在custom-d3-tree.component.ts中声明css文件的?
  • 好问题,我将发布custom-d3-tree.component.ts 文件。
  • 您是否尝试过内联声明 CSS 属性?
  • 这可能是由于以下问题stackoverflow.com/questions/36214546/…
  • 我尝试在 html 标记中使用 style= 声明 CSS 属性,但我不知道其中许多的等效格式。我知道&lt;li style="list-style: 'none';"&gt;,但我能为div:hover { overflow-y: scroll; }做什么?

标签: javascript html css angular d3.js


【解决方案1】:

默认根据https://angular.io/guide/component-styles

@Component 元数据中指定的样式仅适用于 该组件的模板。

这样你就可以只为这个元素设置样式

<div class="d3-chart" #chart></div>

使用以下选择器 div 或 .d3.chart。

如果您想将样式的可见性传播到组件的所有子元素,那么您应该考虑使用 :host 和 ::ng-deep 选择器的组合,即:

:host ::ng-deep .wordwrap {
  ...
}

:host ::ng-deep div:hover {
  ..
}

我会避免使用div 选择器,而是给他们一个类。

还有一个关于我们如何设置 Angular 组件样式的精彩概述:

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    相关资源
    最近更新 更多