gpt4 book ai didi

angular - 安装@type/vis 和 vis 后定义 DataView 时发生冲突

转载 作者:行者123 更新时间:2023-12-04 10:29:04 26 4
gpt4 key购买 nike

js 和 angular 在我的网页上显示一些图表。我安装了

npm install vis 
npm install @type/vis

但我发现两者都有一个名为 DataView 的函数。
我想使用 vis 的 DataView 但它似乎有 Angular 自动找到 @type/vis 的 DataView。

我的代码是这样的:

import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Network, DataSet, DataView} from 'vis';

@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss']

})
export class TestComponent implements AfterViewInit {
@ViewChild('network', {static: false}) el: ElementRef;
@ViewChild('nodeFilterSelect', {static:false}) nodeFilter: ElementRef;
@ViewChild('edgesFilter', {static: false}) edgeFilter: ElementRef;
private networkInstance: any;

ngAfterViewInit() {
const nodes_set = new DataSet<any>([
{ id: 1, label: 'Eric Cartman', age: 'kid', gender: 'male' },
{ id: 2, label: 'Stan Marsh', age: 'kid', gender: 'male' },
{ id: 3, label: 'Wendy Testaburger', age: 'kid', gender: 'female' },
{ id: 4, label: 'Mr Mackey', age: 'adult', gender: 'male' },
{ id: 5, label: 'Sharon Marsh', age: 'adult', gender: 'female' }
]);

const edges_set = new DataSet<any>([
{ from: 1, to: 2, relation: 'friend', arrows: 'to, from', color: { color: 'red'} },
{ from: 1, to: 3, relation: 'friend', arrows: 'to, from', color: { color: 'red'} },
{ from: 2, to: 3, relation: 'friend', arrows: 'to, from', color: { color: 'red'} },
{ from: 5, to: 2, relation: 'parent', arrows: 'to', color: { color: 'green'} },
{ from: 4, to: 1, relation: 'teacher', arrows: 'to', color: { color: 'blue'} },
{ from: 4, to: 2, relation: 'teacher', arrows: 'to', color: { color: 'blue'} },
{ from: 4, to: 3, relation: 'teacher', arrows: 'to', color: { color: 'blue'} },
]);


const nodesView = new DataView(nodes_set, {})
const edgesView = new DataView(edges_set, {})



const container = this.el.nativeElement;


const data = { nodes: nodes_set, edges: edges_set };
[![enter image description here][1]][1]
this.networkInstance = new Network(container, data, {});

}
}


你可以看到我从 vis 导入了 DataView,但我们把光标放在了 DataView 上,它是这样的:

enter image description here

@type/vis 中的 DataView 是:
export class DataView<T extends DataItem | DataGroup> {
length: number;
constructor(items: T[]);
}



因此,即使我从 vis 导入它,似乎也有 Angular 地找到 @type/vis 的 DataView
这个怎么解决??

最佳答案

这就是类型的工作方式。他们确保您编写正确的代码。里面的包裹@typesvis 添加类型提示包,它不提供自己的类型。要修复您的错误,您需要传递一个数组,而不是一个对象:

const nodesView = new DataView(nodes_set);
const edgesView = new DataView(edges_set);

我对它进行了更深入的研究,并意识到包 vis已弃用,不再维护。还有 @types/vis不应再使用,因为新项目 vis.js 提供自己的类型。因此,要开始为您服务,您需要执行以下操作:
npm r vis @types/vis
npm i vis-data

然后,您将获得正确的包类型。例如 DataView构造函数具有以下类型:
constructor(data: DataInterface<Item, IdProp>, options?: DataViewOptions<Item, IdProp>);

所以你可以保持你的代码相同:
const nodesView = new DataView(nodes_set, {});
const edgesView = new DataView(edges_set, {});

其中第二个参数是 DataViewOptions具有以下类型:
export interface DataViewOptions<Item, IdProp extends string> {
fieldId?: IdProp;
filter?: (item: Item) => boolean;
}

作为旁注,我看到您正在使用 angular,vis 包也有自己的 angular 包装器。也许你可以看看它是否适合你的项目: https://github.com/visjs/ngx-vis

关于angular - 安装@type/vis 和 vis 后定义 DataView 时发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60495574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com