事实上,有一种方法可以做你想做的事情,尽管可能不是 Typescript 接口,因为 Günter Zöchbauer 是正确的,一旦代码被转换为 javascript,它们就不存在了。
但是,您可以使用父类。父类可能是一个抽象类。现在我想了想,如果接口被转译到运行时命名空间中,它们也应该可以工作,我不知道它们是否是。
@Component({
selector: 'square',
providers: [provide(Shape, useExisting: forwardRef( ()=>Square )]
})
class Square extends Shape {}
请参阅此讨论。
https://github.com/angular/angular/issues/8580
现在我想为像我这样使用 es5 的人留下我自己的示例,并且为了更彻底的用例演示。我试图平衡额外细节的数量,以使该示例作为一个整体有意义而不会变得无关紧要。
如果您因为偏离主题而对我投反对票,请停止阅读此处。
我需要在仪表板组件中执行一些自定义调整大小逻辑,并且我希望几种不同类型的图表指令仅在我在父仪表板组件中执行自定义调整大小逻辑后重新呈现它们自己。我的一些图表实际上是组件,它没有引起任何问题。使以下模式在 es5 中工作所需的任何其他内容都是标准的。你不需要在提供给你的 NgModule 的提供者列表中包含 app.Renderable。
renderable.class.js
(function(app) {
app.Renderable = ng.core.Class({
constructor : [function Renderable() {}],
render : function() {}
});
})(window.app || (window.app = {}));
chart-one.directive.js
(function(app) {
app.ChartOneDirective = ng.core.Directive({
selector : 'canvas[chart-one]',
inputs : ['config:chart-one'],
providers : [{
provide: app.Renderable,
useExisting: ng.core.forwardRef(function(){
return app.ChartOneDirective;
}),
}]
}).Class({
extends : app.Renderable,
constructor : [/* injections */ function ChartOneDirective(/* injections */) {
// do stuff
}],
// other methods
render : function() {
// render the chart
}
});
})(window.app || (window.app = {}));
chart-two.directive.js
(function(app) {
app.ChartTwoDirective = ng.core.Directive({
selector : 'canvas[chart-two]',
inputs : ['config:chart-two'],
providers : [{
provide: app.Renderable,
useExisting: ng.core.forwardRef(function(){
return app.ChartTwoDirective;
}),
}]
}).Class({
extends : app.Renderable,
constructor : [/* injections */ function ChartTwoDirective(/* injections */) {
// do stuff
}],
// other methods
render : function() {
// render the chart
}
});
})(window.app || (window.app = {}));
dashboard.component.js
(function(app) {
app.DashboardComponent = ng.core.Component({
selector : 'dashboard-component',
templateUrl : 'components/dashboard/dashboard.component.html',
host : {
'(window.resize)' : 'rerender()',
},
queries : {
renderables : new ng.core.ViewChildren(app.Renderable),
// other view children for resizing purposes
}
}).Class({
constructor : [/* injections */ function DashboardComponent(/* injections */) {
// do stuff
}],
resize : function() {
// do custom sizing of things within the dom
},
// other methods
rerender : function() {
this.resize();
this.renderables.forEach(function(r){
r.render();
});
}
});
})(window.app || (window.app = {}));
dashboard.component.html
<div #sizeMe>
<div class='canvas-wrapper'><canvas [chart-one]></canvas></div>
<div class='canvas-wrapper'><canvas [chart-two]></canvas></div>
<div class='canvas-wrapper'><canvas [chart-one]></canvas></div>
<div #sizeMeToo>
<div class='canvas-wrapper'><canvas [chart-two]></canvas></div>
<div class='canvas-wrapper'><canvas [chart-one]></canvas></div>
</div>
</div>
现在,在 es5 javascript 中,实际上没有必要扩展 Renderable 类以使其工作。此外,您可以在您的提供者列表中放置多个提供者,从而允许为我的多个令牌查询您的组件或指令。因此,您可以说您可以“实现”几个“接口”,以便以经典的 javascript 方式进行 ViewChild 选择,实际上没有任何保证。