在我的组件 keydown
事件处理程序中,我需要通过this
修改组件的属性:
canvasKeyHandler (event) {
console.log('vrscene.canvasKeyHandler: event.keyCode=' + event.keyCode);
console.log('vrscene.canvasKeyHandler: self.dolly' + this.dolly);
CameraKeypressEvents.keyHandler(event, this.dolly) // <-- do something with this.dolly
但this.dolly
设置为undefined
:
vrscene.canvasKeyHandler: self.dollyundefined
vrscene.html:
<div class="container" id ="canvas-container-2">
<canvas id="vrruntime-view"
class="camera-keypress-events"
tabindex="1"
(keydown)="canvasKeyHandler($event)"
style="border: 1px solid black;"
>
</canvas>
<!--camera-keypress-events [cubeScene]="cubeScene" -->
</div>
vrscene.ts:
import {Component} from 'angular2/core';
import {Injectable} from 'angular2/core';
import WebGLRenderer = THREE.WebGLRenderer;
import {VRRenderer} from '../vrrenderer/vrrenderer'
import {CameraKeypressEvents} from '../camera-keypress-events/camera-keypress-events'
import Object3D = THREE.Object3D;
import Scene = THREE.Scene;
import PerspectiveCamera = THREE.PerspectiveCamera;
import Mesh = THREE.Mesh;
import VRControls = THREE.VRControls;
import VREffect = THREE.VREffect;
@Component ({
selector: 'vrscene',
templateUrl: 'app//vrscene/vrscene.html',
})
@Injectable()
export class VRScene {
private _scene: Scene;
camera: PerspectiveCamera;
dolly: Object3D;
vrControls: VRControls;
vrEffect: VREffect;
webVrManager;
sphere: Mesh;
cube: Mesh;
BaseRotation = new THREE.Quaternion();
constructor() {}
//initScene(width: number, height: number, renderer: VRRenderer) {
init(width: number, height: number, vrRenderer: VRRenderer) {
console.log('VRScene.init: entered')
this.scene = new THREE.Scene;
this.camera = new THREE.PerspectiveCamera(75, width / height);
this.camera.position.set(0, 1.5, 100);
this.dolly = new THREE.Object3D();
this.dolly.position.z = 50;
this.scene.add(this.dolly);
//
this.dolly.add(this.camera);
this.vrControls = new THREE.VRControls(this.camera);
this.vrEffect = new THREE.VREffect(vrRenderer.renderer);
this.vrEffect.setSize(width, height);
this.webVrManager = new (<any>window).WebVRManager(vrRenderer.renderer, this.vrEffect);
console.log('VRScene.init: this.webVrManager=' + this.webVrManager);
this.camera.quaternion.copy(this.BaseRotation);
var geometry = new THREE.BoxGeometry(25, 25, 25);
var meshParms = new Object();
meshParms['color'] = 0xff8000;
var material = new THREE.MeshBasicMaterial(meshParms);
//material = new THREE.MeshBasicMaterial( {color: 0xffff00, side: THREE.DoubleSide} );
this.cube = new THREE.Mesh(geometry, material);
this.scene.add(this.cube);
// draw!
vrRenderer.canvas.focus();
//CubeOnPlaneScene.prototype.mainLoop.bind(this)
// bind the 'this' of the canvasKeyHandler to the definition-time 'this'
//VRScene.prototype.canvasKeyHandler.bind(this)
}
canvasKeyHandler (event, dolly) {
console.log('vrscene.canvasKeyHandler: event.keyCode=' + event.keyCode);
//console.log('vrscene.canvasKeyHandler: this.dolly' + this.dolly);
//console.log('vrscene.
canvasKeyHandler: self.dolly' + this.dolly);
console.log('vrscene.canvasKeyHandler: dolly' + dolly);
//CameraKeypressEvents.keyHandler(event, this.dolly)
//CameraKeypressEvents.keyHandler(event, VRScene.prototype.canvasKeyHandler)
CameraKeypressEvents.keyHandler(event, dolly)
}
doIt() : string {
return 'hello from VRScene'
}
// getters and setters
get scene():Scene {
return this._scene;
}
set scene(scene: Scene) {
if (scene === undefined) throw 'Please supply a scene';
this._scene = scene;
}
}
密钥处理程序:
import {Directive} from 'angular2/core';
import {CubeScene} from '../cube-scene/cube-scene';
import Object3D = THREE.Object3D;
import Vector3 = THREE.Vector3;
import {Base} from '../base/base';
import Quaternion = THREE.Quaternion;
@Directive({
selector: '[camera-keypress-events]',
providers: [],
//host: {},
host: {
'(keypress)' : 'onKeypress($event)'
},
})
export class CameraKeypressEvents {
constructor() {}
static CAMERA_MOVE_DELTA = 1.2;
static CAMERA_ROT_DELTA = 5;
static keyHandler (event, dolly: Object3D) {
console.log('CameraKeypressEvents.keyHandler: event.keyCode=' + event.keyCode)
console.log('CameraKeypressEvents.keyHandler: dolly=' + dolly)
switch( event.keyCode) {
case 'S'.charCodeAt(0):
console.log('you pressed s');
//dolly.position.z += CAMERA_MOVE_DELTA;
dolly.translateZ(this.CAMERA_MOVE_DELTA);
console.log('dolly.postion.x=' + dolly.position.x);
break;
case 'W'.charCodeAt(0):
//console.log('you pressed s');
//this.dolly.position.z -= this.CAMERA_MOVE_DELTA;
dolly.translateZ(-this.CAMERA_MOVE_DELTA);
//console.log('this.do-ly.postion.x=' + this.dolly.position.x);
break;
case 'A'.charCodeAt(0):
//this.dolly.position.x -= this.CAMERA_MOVE_DELTA;
dolly.translateX(-this.CAMERA_MOVE_DELTA);
break;
case 'D'.charCodeAt(0):
//console.log('you pressed s');
//this.dolly.position.x += this.CAMERA_MOVE_DELTA;
dolly.translateX(this.CAMERA_MOVE_DELTA);
//console.log('this.dolly.postion.x=' + this.dolly.position.x);
break;
case 'N'.charCodeAt(0):
//this.dolly.position.y -= this.CAMERA_MOVE_DELTA;
dolly.translateY(-this.CAMERA_MOVE_DELTA);
break;
case 'P'.charCodeAt(0):
//console.log('you pressed s');
//this.dolly.position.y += this.CAMERA_MOVE_DELTA;
//console.log('this.dolly.postion.x=' + this.dolly.position.x);
dolly.translateY(this.CAMERA_MOVE_DELTA);
break;
case 'Q'.charCodeAt(0):
var tmpQuat = (new THREE.Quaternion()).setFromAxisAngle( new THREE.Vector3(0,1,0), Base.ONE_DEG * this.CAMERA_ROT_DELTA);
dolly.quaternion.multiply(tmpQuat);
break;
case 'E'.charCodeAt(0):
var tmpQuat = (new THREE.Quaternion()).setFromAxisAngle( new THREE.Vector3(0,1,0), Base.ONE_DEG * -this.CAMERA_ROT_DELTA);
dolly.quaternion.multiply(tmpQuat);
break;
};
}
onKeypress (event, cubeScene) {
console.log('CameraKeypressEvents.onKeypress: event.keyCode=' + event.keyCode)
//event.preventDefault();
console.log('vtClass.canvasKeyhandler: cubeScene=' + cubeScene);
console.log(event, event.keyCode, event.keyIdentifier);
/*
*/
}
}
我如何:
A)在keyhandler调用上传递实例?
我试过了:
(keydown)="canvasKeyHandler($event,{{dolly}})"
B)将canvasKeyHandler
方法绑定到编译时间this
?
我试过了:
// bind the callback 'this' of the canvasKeyHandler to the definition-time 'this'
VRScene.prototype.canvasKeyHandler.bind(this)
和
canvasKeyHandler (event) {
...
}.bind(this)
如果这是一个显而易见的问题,我表示道歉,但我只是学习Angular 2框架并且在这一点上有点不知所措。我需要对@Input做什么吗?
非常感谢
答案 0 :(得分:2)
(keydown)="canvasKeyHandler($event,{{dolly}})"
应该是
(keydown)="canvasKeyHandler($event,dolly)"
我不知道这是否真正解决了您的问题(不能真正理解问题所在)
答案 1 :(得分:1)
this
对应于事件处理程序(canvasKeyHandler
)中组件的实例。我认为使用以下命令配置keydown
事件的处理程序就足够了:
(keydown)="canvasKeyHandler($event)"
我看到您在dolly
方法中初始化init
属性。但我看不出这个方法的调用方法。也许这就是你问题的原因......
答案 2 :(得分:0)
对于我而言,这是DI的微妙之处。
基本上,此问题可归因于用户错误。没有任何冒烟问题,angular2或打字稿不能正常工作。我的主要问题是使用'init'方法初始化我的组件而不是在构造函数中执行它。我之所以这样做,是因为我希望延迟绑定属性,直到运行时才能知道。这很好,但看起来注入器没有驱动我的初始化代码(或者它可能是并且当时没有定义属性)。还有一个因素是让一个组件使用继承对父类进行子类化,而不是依赖注入。看起来注入器可能只是驱动父类的构造函数而不是孩子的构造函数,尽管我无法明确地说这是因为我有几个问题正在发生。
我正在重构我的代码以仅使用DI,并将所有初始化放在构造函数中,并且整体上只是对控制DI过程有点紧张。
对于遇到类似问题的人来说,主要的意思是要意识到组件可能存在或创建的三个潜在上下文:编译时,注入时和运行时。甚至可能有加载时间,但我假设这与注入时间相同。 “通常”,它们都是一样的,你不必区分它们,但是如果你正在做后期动态初始化,和/或使用继承代替DI,请记住,注入的组件可能不会处于您期望的状态。