【问题标题】:Angular: PrimeNG Organization Chart not rendering the chart properlyAngular:PrimeNG 组织结构图未正确呈现图表
【发布时间】:2020-10-24 23:49:18
【问题描述】:

我已经按照我的要求实施了 PrimeNG 的组织结构图。我来自 TreeNode 类型数组的数据在 UI 中正确呈现,但设计不是。 以下是我的代码:

app.module.ts

import {OrganizationChartModule} from 'primeng/organizationchart';
imports: [ OrganizationChartModule ]

thankyou.component.ts

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { ApplicationService } from '../service-application/application.service';
import {TreeNode} from 'primeng/api';

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

export class ThankyouComponent implements OnInit {

  totObtainedMarks: number = 0;
  totTotalMarks: number = 0;
  marks: TreeNode[];
  constructor(public appService: ApplicationService) {}

  ngOnInit() {
    let result = [];
    for(let i of this.appService.result) {
      let mark: any;
      this.totObtainedMarks += i.obtainedMarks;
      this.totTotalMarks += i.totalMarks;
      mark = {
        label: i.questionCategory + ': ' + i.obtainedMarks.toString() + '/' + this.totTotalMarks.toString()
      };
      result.push(mark);
    }
    this.marks = [{
      label: 'Total: ' + this.totObtainedMarks.toString() + '/' + this.totTotalMarks.toString(),
      children: result
    }];
  }
}

thankyou.component.html

<p-organizationChart [value]="marks"></p-organizationChart>

我得到的观点:

我按照官方文档进行了创建,但仍然无法理解问题所在。 请帮帮我。

【问题讨论】:

  • 确保 ThankyouComponent 在 app.module 中声明,而不是在其他模块中,您是否看到任何控制台错误?
  • 没有控制台错误并且组件被声明,“也不是其他模块”是什么意思?
  • 我的意思是“而不是任何其他模块”对不起也是一个错误,只要在 app.moduel 中声明了“ThankYouComponent”,它看起来对我来说很好。
  • 是的,这就是我感到困惑的原因。只有样式没有呈现:(
  • @Avishek 您是否为任何 PrimeNG 组件添加了所需的 CSS 引用?

标签: angular user-interface frontend primeng angular9


【解决方案1】:

当我在 StackBlitz 上查看 PrimeNG 组织结构图的最基本示例并查看 angular.json 时,需要添加一些样式参考以使其正常工作。

StackBlitz example 没有样式

您可以清楚地看到,有些地方不对劲,而且看起来和您的问题完全一样。

带有样式的 StackBlitz 示例 angular.json

angular.json 文件中添加(或不注释掉)这些 CSS 引用行时,正确的样式将应用于您的组织结构图。

"styles": [
    "src/styles.css",
    "node_modules/primeng/resources/themes/nova-light/theme.css",
    "node_modules/primeng/resources/primeng.min.css",
    "node_modules/primeicons/primeicons.css"
],

有关工作示例,请参阅 this updated StackBlitz,包括应用的正确样式和浏览器输出中的良好分层结构。

入门指南

当进一步挖掘时,它甚至在'Get Started' guide of PrimeNG中被提及。

依赖项 大多数 PrimeNG 组件 (95%) 是本机组件,并且有一些例外情况具有 3rd 方依赖项。此外,组件需要 PrimeIcons 的图标。

css依赖如下,Prime Icons,你选择的主题和组件的结构css。

在这里,您可以选择通过 HTML 引用或通过 Angular CLI 包含样式(就像我们在上面所做的那样):

<link rel="stylesheet" type="text/css" href="/node_modules/primeicons/primeicons.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/themes/nova-light/theme.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/primeng.min.css" />
"styles": [
  "node_modules/primeicons/primeicons.css",
  "node_modules/primeng/resources/themes/nova-light/theme.css",
  "node_modules/primeng/resources/primeng.min.css",
  //...
],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2016-05-09
    • 2015-10-04
    相关资源
    最近更新 更多