该问题表示在使用Angular ThreeJS时,Canvas不存在初始化器。要解决此问题,可以按照以下步骤:
import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core'; import * as THREE from 'three';
@Component({ selector: 'app-scene', templateUrl: './scene.component.html', styleUrls: ['./scene.component.scss'] }) export class SceneComponent implements AfterViewInit { @ViewChild('canvas') private canvasRef: ElementRef;
constructor() { }
ngAfterViewInit(): void { const canvas = this.canvasRef.nativeElement; const renderer = new THREE.WebGLRenderer({canvas}); const scene = new THREE.Scene();
//Add your ThreeJS code here...
renderer.render(scene, camera);
} }
import * as THREE from 'three';
@NgModule({ declarations: [SceneComponent], imports: [BrowserModule], providers: [], bootstrap: [SceneComponent] }) export class AppModule { }