这个问题通常出现在在Angular中使用ThreeJS时创建canvas画布的时候。解决方法是要确保在组件的生命周期ngAfterViewInit()中将canvas画布实例化并添加到DOM中。
示例代码如下:
import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core'; import * as THREE from 'three';
@Component({
selector: 'app-some-component',
template: '',
})
export class SomeComponent implements AfterViewInit {
@ViewChild('canvas') canvasRef: ElementRef
ngAfterViewInit() { const canvas = this.canvasRef.nativeElement; const renderer = new THREE.WebGLRenderer({ canvas }); // 在这里添加ThreeJS场景的代码 } }
上一篇:AngularThreeJSCanvashasnotinitializer
下一篇:Angular填充问题:IE11 - core-js v3.6.5的es.string.split.js方法在分割正则表达式/^|\s+/时解析失败。