简体   繁体   English

TS2345:“HTMLElement”类型的参数不可分配给“ChartItem”类型的参数

[英]TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem'

I'm working on an Angular dashboard development and I found this error that doesn't let me render area chart我正在开发一个 Angular 仪表板,我发现这个错误不允许我渲染面积图

import { AfterViewInit, Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
import { Chart } from 'chart.js';

@Component({
selector: 'app-widget-area',
templateUrl: './area.component.html',
styleUrls: ['./area.component.scss']
})
export class AreaComponent implements AfterViewInit {


 constructor() {}
 ngAfterViewInit() {
    let elem: HTMLElement;
    const ctx = document.getElementById("myChart") as HTMLElement;
     if (ctx) {
        elem = ctx;
        const myChart = new Chart(elem, {
            data: {
                datasets: [
                    { fill: 'origin' },      // 0: fill to 'origin'
                    { fill: '+2' },         // 1: fill to dataset 3
                    { fill: 1 },             // 2: fill to dataset 1
                    { fill: false },         // 3: no fill
                    { fill: '-2' },          // 4: fill to dataset 2
                    { fill: { value: 25 } }    // 5: fill to axis value 25
                ]
            }
        });
    }
}

And got this error:并得到这个错误:

TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem'.
Type 'HTMLElement' is missing the following properties from type 'HTMLCanvasElement': height, width, captureStream, getContext, and 2 more.

I've tried with all the internet stuff and nothing worked for me, anyone have the same error?我已经尝试了所有互联网的东西,但对我没有任何帮助,有人有同样的错误吗? BTW this is my simply html, I have my NgChartModule from charjs and nothing worked for me顺便说一句,这是我的简单 html,我有来自 charjs 的 NgChartModule,但对我没有任何帮助

<div>
  <div style="display: block">
  <canvas id="myChart " ></canvas>

  </div>
</div>

Chart class' constructor take the first parameter ( item ) as ChartItem type where ChartItem takes type of: Chart类的构造函数将第一个参数 ( item ) 作为ChartItem类型,其中ChartItem采用以下类型:

ChartItem: string | CanvasRenderingContext2D | HTMLCanvasElement | { canvas: HTMLCanvasElement } | ArrayLike<CanvasRenderingContext2D | HTMLCanvasElement>

From here , you can see the usage:这里,您可以看到用法:

const ctx = document.getElementById('myChart');
const ctx = document.getElementById('myChart').getContext('2d');
const ctx = $('#myChart');
const ctx = 'myChart';
const ctx = document.getElementById("myChart");
if (ctx) {
    const myChart = new Chart(ctx, {
            data: {
                datasets: [
                    { fill: 'origin' },      // 0: fill to 'origin'
                    { fill: '+2' },         // 1: fill to dataset 3
                    { fill: 1 },             // 2: fill to dataset 1
                    { fill: false },         // 3: no fill
                    { fill: '-2' },          // 4: fill to dataset 2
                    { fill: { value: 25 } }    // 5: fill to axis value 25
                ]
            }
        });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个相等的文件输入有时会抛出 TS2345:'FileList | 类型的参数 null' 不能分配给“文件 []”类型的参数,有时可以工作 - Two equal file inputs sometimes throw TS2345: Argument of type 'FileList | null' is not assignable to parameter of type 'File[]' and sometimes work 类型&#39;()=&gt; JQuery&#39;的参数不能分配给类型&#39;()=&gt; boolean&#39;的参数 - Argument of type '() => JQuery' is not assignable to parameter of type '() => boolean' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' Angular Ivy strictTemplates “Event”类型的参数不可分配给“InputEvent”类型的参数 - Angular Ivy strictTemplates Argument of type 'Event' is not assignable to parameter of type 'InputEvent' Angular 键值 pipe:参数类型不可分配给参数类型 - Angular keyvalue pipe: argument type is not assignable to parameter type 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) 类型“数字”不可分配给类型“字符串”.ts(2322) - Type 'number' is not assignable to type 'string'.ts(2322) console.select()期望的HTMLElement类型的参数 - Argument of HTMLElement type expected by console.select() “字符串”类型的参数不可分配给“A”类型的参数 | 尝试将可能的值分配给字符串时的“B”' - Argument of type 'string' is not assignable to parameter of type '"A" | "B"' when trying to assign possible values to a string 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM