我有课Layer
:
import {AfterViewInit, ViewChild} from 'angular2/core';
export class Layer implements AfterViewInit {
@ViewChild('element') element;
public canvas: HTMLCanvasElement = null;
public context: CanvasRenderingContext2D = null;
ngAfterViewInit() {
this.canvas = this.element.nativeElement;
this.context = this.canvas.getContext('2d');
}
}
我将使用我的组件进行扩展Lines
:
import {Component} from 'angular2/core';
import {Layer} from './layer';
import {Game} from '../../providers/Game';
@Component({
selector: 'lines-component',
template: require('./template.html'),
styles: [`
canvas {
z-index: 2;
}
`]
})
export class Lines extends Layer {
constructor(public game: Game) {
super();
}
}
如您所见,我必须将Game
服务注入到将继承自Layer
. 但是,我想向Layer
类注入服务,所以我可以用它来创建依赖于服务的方法,而且它不会强迫我每次都自己向组件注入服务。
不用说,向Layer
任何组件注入服务都不起作用。
我正在使用角度 2.0.0-beta.0