简体   繁体   English

在子组件的 ngAfterViewInit() 中调用时,来自父组件的元素上的 getElementById 返回 null

[英]getElementById on element from parent component returns null, when called in ngAfterViewInit() of child component

I am trying to display dynamic number of charts by loading child components from parent component.我试图通过从父组件加载子组件来显示动态数量的图表。

I have created an array barChart in Parent where I have the ids ['Canvas0', 'Canvas1'...] .我在 Parent 中创建了一个数组barChart ,其中的 ids ['Canvas0', 'Canvas1'...] But I am getting following error,但我收到以下错误,

TypeError: Cannot read property 'getContext' of null in var ctx = canvas.getContext("2d");类型错误:无法在var ctx = canvas.getContext("2d");读取 null 的属性“getContext” var ctx = canvas.getContext("2d"); of Child Component.子组件。

Parent HTML:父 HTML:

<div *ngFor="let chart of barChart; let i = index">
    <app-bar-chart [chart]="chart"  style="border: 2px solid;" ></app-bar-chart>
</div>

Child TypeScript:儿童打字稿:

export class BarChartComponent implements OnInit {
  @Input() chart: any;

  constructor() { }

  ngOnInit() {
    console.log(this.chart)
  }

  ngAfterViewInit() {
    //setTimeout(()=>{
    //  this.addchart(this.chart);
    //}, 3000);
    this.addchart(this.chart); 
  }

  public addchart(chartid){
    console.log(chartid);
    var canvas = <HTMLCanvasElement> document.getElementById(chartid);
    console.log(document.getElementById(chartid));
    var ctx = canvas.getContext("2d");
    var mychart = new Chart(ctx, {
      type: 'bar',
      data: {
          labels: ['Red', 'Blue'],
          datasets: [{
              label: '# of Votes',
              data: [12, 19],
              backgroundColor: 'lightgreen',
              borderColor: 'blue',
              borderWidth: 1
          }]
      },
      options: {
          legend: {
            display: false
        },
      }
    });
  }
}

Child HTML:子 HTML:

<div *ngIf="chart">
  <canvas id="{{chart}}" width="400" height="400">{{chart}}</canvas>
</div>

you can use @ViewChild decorator to query an element from component template您可以使用@ViewChild装饰器从组件模板中查询元素

BarChartComponet条形图组件

  @ViewChild('chartElm', { static: true }) public chartElm: ElementRef;

  ngAfterViewInit() {
    this.addchart(); 
  }

  public addchart(){
    var canvas = <HTMLCanvasElement> this.chartElm.nativeElement; // 👈

    var ctx = canvas.getContext("2d");
    var mychart = new Chart(ctx, {
      type: 'bar',
      data: {
          labels: ['Red', 'Blue'],
          datasets: [{
              label: '# of Votes',
              data: [12, 19],
              backgroundColor: 'lightgreen',
              borderColor: 'blue',
              borderWidth: 1
          }]
      },
      options: {
          legend: {
            display: false
        },
      }
    });
  }

template模板

<div style="width:400px; height:100%;"> 
  <canvas #chartElm ></canvas>
</div>

demo 🚀🚀演示🚀🚀

暂无
暂无

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

相关问题 从父组件调用时,angular 2 变量在子组件方法中返回 null - angular 2 variable returns null in child component method when called from parent component 当子组件调用方法时,父引用为null-ReactJS - Parent refs are null when method is being called by child component - ReactJS Angular 8 - @ViewChild 在父组件上返回未定义。 (没有嵌套的 ngIf 并在 ngAfterViewInit 中调用) - Angular 8 - @ViewChild returns undefined on parent component. (no nested ngIf and called in ngAfterViewInit) reactjs - State 变量是 null 当从子组件调用父组件中的回调时 - reactjs - State variable is null when callback in parent component is called from child React hooks:在子组件中调用dispatch时,父组件不更新 - React hooks: Parent component not updating when dispatch is called in a child component 从外部jscript调用document.getElementById()返回null - document.getElementById() returns null when called from external jscript 从父组件 React JS 调用 map function 时如何更新子组件的 state - How to update state of child component when it get called in map function from parent component React JS 使用getElementById选择父组件中的元素-Angular 5 - Using getElementById to select element in parent component - Angular 5 将元素从子组件移动到其父组件 - Move element from child component to its parent 从父组件传递道具在子组件 React 中变为空 - Passing a prop from a Parent component becomes null in the Child component React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM