我已经通过以下
方式安装了我需要的依赖项:vis.js: npm install vis --save
@types/vis:npm install @types/vis --save-dev
代码片段:
import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Network, DataSet } from 'vis';
@Component({
selector: 'test',
template: '<div #network></div>'
})
export class TestComponent implements AfterViewInit {
@ViewChild('network') el: ElementRef;
private networkInstance: any;
ngAfterViewInit() {
const container = this.el.nativeElement;
const nodes = new DataSet<any>([
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}
]);
const edges = new DataSet<any>([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
const data = { nodes, edges };
this.networkInstance = new Network(container, data);
}
}
Run Code Online (Sandbox Code Playgroud)
我像上面那样尝试只是为了尝试,但它给了我这个错误:
无法读取未定义的属性“解决”
我错过了什么?
小智 6
我在 Angular 项目中使用 Vis.js Network 模块时遇到了同样的问题。经过一番搜索,我发现问题是由 Network 对象的构造方式引起的。如果替换此行:
this.networkInstance = new Network(container, data);
Run Code Online (Sandbox Code Playgroud)
...用这一行:
this.networkInstance = new Network(container, data, {});
Run Code Online (Sandbox Code Playgroud)
那么问题就解决了。
背景
Vis.js 的 Typescript 类型定义在构造函数的定义中存在错误,该定义声称
“选择”参数的构造是可选的:
constructor(container: HTMLElement, data: Data, options?: Options);。
但是,如果您查看 Vis.js 代码,您会注意到传递未定义的选项属性会导致重要的初始化代码被跳过,从而导致您遇到的错误。
如果相反,您传递了一个空的选项对象,则网络将正确初始化。
| 归档时间: |
|
| 查看次数: |
3356 次 |
| 最近记录: |