繁体   English   中英

Angular 2依赖项注入:将主机作为父类

[英]Angular 2 dependency injection: Host as parent class

我有一个快速的问题:我可以将Interface或抽象类(或至少是父类)绑定为组件的@Host()吗? 好像DI系统无法解决多态性。

我有这样的界面:

export interface Component {
}

和子组件从此接口:

...
export class SomeComponent implements Component {
...
}

现在,我想创建指令,并且即使我将此指令放在SomeComponent上,也要使用Component作为主机。

喜欢:

constructor(private host: Component) { }

实际上,在运行时没有对应的TypeScript接口。 这意味着您不能将它们用作类型。

如果您在服务中尝试此操作:

export interface SomeInterface {
  someMethod();
}

export class HeroService implements SomeInterface {
  (...)
}

尝试从另一个模块导入时,我们将具有undefined:

import {HeroService,SomeInterface} from './hello.service';

console.log('service = '+HeroService); // <---- not null
console.log('interface = '+SomeInterface); // <---- undefined

这是一个描述这个的插件: https ://plnkr.co/edit/RT59B0tw40lnq85XMMi7?p = preview。

希望对您有帮助,蒂埃里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM