javascript - main.js:1 错误类型错误:无法读取 null 的属性“querySelectorAll”
问题描述
我正在使用带有角度的 Chartist 来创建图表。问题是当我运行服务器时我的图表不显示,并且我在控制台上收到此错误。当我刷新页面时,图表会出现。我该如何解决这个问题?我在控制台上打印了数据以检查它们是否正确。
line.component.ts 文件
import { Component, OnInit, Input, ViewEncapsulation } from '@angular/core';
import { ChartData } from '../chart.data.namespace';
import * as Chartist from 'chartist';
import { NChartist } from '../NChartist.namespace';
import {
IBarChartOptions,
IChartistAnimationOptions,
IChartistData
} from 'chartist';
@Component({
selector: 'app-line',
templateUrl: './line.component.html',
styleUrls: ['./line.component.css'],
encapsulation:ViewEncapsulation.None,
})
export class LineComponent implements OnInit {
chartline: any;
@Input() data: ChartData.LabelValue[];
constructor() {
}
ngOnInit() {
this.createLineChart();
}
createLineChart() {
let linedata: NChartist.Data = new NChartist.Data();
this.prepareLineData(linedata);
const prepared_l_data = {
series: [linedata.series],
labels: linedata.labels,
}
this.chartline = new Chartist.Line('.ct-chart-line', prepared_l_data,{})
console.log(this.chartline);
this.chartline.update();
this.startAnimationForLineChart(this.chartline);
}
prepareLineData(linedata) {
for (let i = 0; i < this.data.length; i++) {
linedata.series[i] = this.data[i].value;
linedata.labels[i] = this.data[i].label;
}
}
startAnimationForLineChart(chart){
let seq: any, delays: any, durations: any;
seq = 0;
delays = 80;
durations = 500;
chart.on('draw', function(data) {
if(data.type === 'line' || data.type === 'area') {
data.element.animate({
d: {
begin: 600,
dur: 700,
from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
to: data.path.clone().stringify(),
easing: Chartist.Svg.Easing.easeOutQuint
}
});
} else if(data.type === 'point') {
seq++;
data.element.animate({
opacity: {
begin: seq * delays,
dur: durations,
from: 0,
to: 1,
easing: 'ease'
}
});
}
});
seq = 0;
};
}
line.component.html
<body>
<div class="card card-chart">
<div class="card-header card-header-warning">
<div class="ct-chart-line" id="chartline"></div>
</div>
<div class="card-body">
<h4 class="card-title">Line Chart</h4>
<p class="card-category">
<span class="text-success"><i class="fa fa-long-arrow-up"></i> 55% </span> increase in today
sales.
</p>
</div>
<div class="card-footer">
<div class="stats">
<i class="material-icons">access_time</i> updated 4 minutes ago
</div>
</div>
</div>
</body>
解决方案
当您调用 createLineChart() 时,您必须确保所有 html 都是完整的。ngOnInit() 不是调用查询选择器的最佳循环钩子。尝试在 ngAfterViewInit() 方法中调用它,而不要忘记实现 AfterViewInit :
export class LineComponent implements OnInit, AfterViewInit
ngAfterViewInit(){
this.createLineChart();
}
推荐阅读
- angular - HighChart 亮点
- python - 将使用 sakila 数据库生成加拿大城市的脚本?
- java - 预热会加快在 Java 中发送 http 请求的速度吗?
- html - 背景变成白色而不是透明(引导程序)
- svn - 如何从 CLI 提交 svn propset?
- java - 无法使用 Selenium ChromeDriver SerenityBDD 设置默认下载路径
- git - 有没有办法根据 repo 克隆中可用的信息来检测 Git 存储库的提供者?
- ssl - haproxy 显示很多 ssl 握手失败并且仍然加载页面
- python - 只读取符合条件的 .parquet 文件的特定行?
- python - Num_workers > 0 在 SLURM 中创建内存错误?