【发布时间】:2020-06-12 21:59:43
【问题描述】:
您好,我是 AngularJs 的新手,最近我们回顾了一些 Angular 代码,我看到的是这样的:
import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Network, DataSet } from 'vis';
@Component({
selector: 'app-test',
template: '<div #network></div>'
})
export class TestComponent implements AfterViewInit {
@ViewChild('network', {static: false}) 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, {});
}
}
参考是:How to make Network visualization work in vis.js with Angular?
所以我很困惑什么是 @ViewChild('network', {static: false}) el: ElementRef; 据我了解,viewchild 通常用于父组件调用子组件。但在这个例子中,我尝试运行代码,它在没有定义子组件的情况下工作。所以这里的 ElementRef 是“孩子”?
【问题讨论】:
标签: angular