简体   繁体   English

Cytoscape.js 图形不在嵌套的 angular 组件中运行。 错误 - 无法读取 null 的属性“className”

[英]Cytoscape.js Graph does not run in the nested angular component. Error - Cannot read property 'className' of null

I am trying to create a separate angular component for a graph built in cytoscape.我正在尝试为 cytoscape 中内置的图形创建一个单独的 angular 组件。 Now I am trying to use the same component in the other component in Angular but it does not do anything.现在我试图在 Angular 的另一个组件中使用相同的组件,但它没有做任何事情。 It also gives me an error Error - Cannot read property 'className' of null它还给我一个错误错误 - 无法读取 null 的属性“className”

Here is the Stackblitz:这是 Stackblitz:

https://stackblitz.com/edit/angular-xvrntx?file=src%2Fapp%2Fdata-cytoscape%2Fdata-cytoscape.component.html https://stackblitz.com/edit/angular-xvrntx?file=src%2Fapp%2Fdata-cytoscape%2Fdata-cytoscape.component.html

Click on graph and open console单击图形并打开控制台

From angular-material doc来自angular-material 文档

Lazy Loading By default, the tab contents are eagerly loaded.延迟加载 默认情况下,选项卡内容是急切加载的。 Eagerly loaded tabs will initalize the child components but not inject them into the DOM until the tab is activated急切加载的选项卡将初始化子组件,但在激活选项卡之前不会将它们注入 DOM

The problem is that cy div is not being attached to DOM until user activates the tab but DataCytoscapeComponent component is getting initialized and trying to access cy div which is the cause of the error, To solve this you can lazy load DataCytoscapeComponent component which will cause the component to not initialize until tab is activated by user.问题是cy div 在用户激活选项卡之前不会附加到DOM ,但DataCytoscapeComponent组件正在初始化并尝试访问cy div,这是导致错误的原因,要解决此问题,您可以lazy load DataCytoscapeComponent组件,这将导致组件在用户激活选项卡之前不会初始化。

<mat-tab label="Graph">
  <ng-template matTabContent>
    <app-data-cytoscape></app-data-cytoscape>
  </ng-template>
</mat-tab>

Here the working example这里是工作示例

Note: Graph was not visible when tab clicked because the height is getting set to 0 so i have set default min-height in component css and removed absolute positioning.注意:单击选项卡时图表不可见,因为height设置为 0,因此我在组件 css 中设置了默认min-height并删除了absolute定位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM