main.js:1 ERROR TypeError: Cannot read property 'querySelectorAll' of null

main.js:1 ERROR TypeError: Cannot read property 'querySelectorAll' of null

我正在使用 Chartist 和 angular 创建图表。问题是当我 运行 服务器时我的图表不显示,我在控制台上收到此错误。当我刷新页面时,会显示图表。我该如何解决这个问题? 我在控制台上打印了数据,以检查它们是否正确,它们是否正确。

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();
}