【问题标题】:Working with Highcharts using @ViewChildren使用 @ViewChildren 处理 Highcharts
【发布时间】:2017-06-09 20:22:52
【问题描述】:

在Angular 2中有一个如何使用Highcharts(基本折线图)的例子:https://angular2highcomponents.azurewebsites.net/LineCharts/BasicLine

我想知道如何动态实现这一点,但不知道我将绘制多少张图表。这是我尝试过的(但失败了):

在我的 component.ts 中:

@ViewChildren('chart') components: QueryList<any>;
Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course

在我的 component.html 中:

<div *ngFor="let widget of widgets" class="col-xs-12 col-sm-6 placeholder2">
        <div #chart style="width:100%; height:300px;"></div>
      </div>

我得到这个错误:

TypeError: 无法读取未定义的属性“nativeElement”

我收到此错误是因为组件数组未初始化但使用 viewChild,这不是问题(如您在示例中所见)

提前致谢。

【问题讨论】:

    标签: angular highcharts


    【解决方案1】:

    你可以尝试几件事,我将它们全部分组:

    import { Component, OnInit, AfterViewInit, AfterViewChecked } from '@angular/core';
    
    export class YourClass, implements OnInit, AfterViewInit, AfterViewChecked {
        ngOnInit() {
            Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
        }
        ngAfterViewInit() {
            Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
        }
        ngAfterViewChecked() {
            Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
        }
    }
    

    试试AfterViewChecked first, it should work.

    【讨论】:

    • 不幸的是,它们都不起作用。特别是当我尝试 AfterViewChecked 时,它陷入了无限循环:(
    • 你可以尝试其中任何一个,但有超时吗?您的问题是它找不到 HTML 元素,因此找不到 nativeElement 属性。要么您没有正确获取它(但是使用 ViewChild ......这很难),或者当您尝试获取它时它没有加载。
    • 我也试过超时,还是不行。将尝试以不同方式超时的版本。
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多