如何将泛型类型参数传递给 Angular2 组件? [英] How can I pass a generic type parameter to an Angular2 component?
本文介绍了如何将泛型类型参数传递给 Angular2 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个具有固定输入参数类型的组件,
Let's say I got a component with a fixed input parameter type,
@Component({
selector: 'fixed',
template: '<div>{{value}}</div>'
})
export class FixedComponent {
@Input() value: string;
}
我如何使该参数类型通用,即
How do I go about making that parameter type generic, i.e.
@Component({
selector: 'generic',
template: '<div>{{value}}</div>'
})
export class GenericComponent<T> {
@Input() value: T;
}
即如何在父组件的模板中传递类型?
That is, how do I pass the type in the template of the parent component?
<generic ...></generic>
推荐答案
似乎在使用 AOT 编译 执行此操作的唯一方法是将泛型类型替换为any".请参阅 https://github.com/angular/angular/issues/11057
It seems that when using AOT compilation the only way to do this is to replace the generic type with 'any'. See https://github.com/angular/angular/issues/11057
这篇关于如何将泛型类型参数传递给 Angular2 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文