Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
The elementId is null after the second time the component has been initialized

I have a component where I will draw a canvas chart. And when I initialize the component for the first time, everything is ok, but if I leave this component and return to it again, then document.getElementById('myChart'); is null and doesn't draw my chart

    ngAfterViewInit() {
    Chart.register(...registerables);
    this.Chart(); 
}


private Chart() {
    const ctx: any = document.getElementById('myChart');
const myChart = new Chart(ctx.getContext('2d'), { // some shit })
}

I tried to do it differently

 const canvas = <HTMLCanvasElement>document.getElementById('myChart');
    const ctx = canvas.getContext('2d');

But I didn't quite understand how to solve this error and eventually came to the first option

Argument of type 'CanvasRenderingContext2D | null' is not assignable to parameter of type 'ChartItem'.   Type 'null' is not assignable to type 'ChartItem'.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I approached this problem from a different angle. First I used ViewChild and applied ElementRef to it

  @ViewChild('myChart') chartRef: ElementRef;
  private myChart: Chart;

And be sure to use everything in ngAfterViewInit since it starts working after building the DOM tree

  ngAfterViewInit(): void {
    this.Chart();
  }

 this.myChart = new Chart(this.chartRef.nativeElement, {some shit})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda