Angular 在typescript中定义抽象类的类型
如何使下面的BaseListComponent的Angular 在typescript中定义抽象类的类型,angular,class,typescript,types,abstract-class,Angular,Class,Typescript,Types,Abstract Class,如何使下面的BaseListComponent的类型可分配 下面的代码引发以下错误: 键入“”({title:string;component:typeof QuestionsListComponent; }|{title:string;component:…'不可分配给类型'{ 标题:字符串;组件:BaseListComponent的类型;}[]” 摘录: abstract class BaseListComponent { protected title } class Weather
类型可分配
下面的代码引发以下错误:
键入“”({title:string;component:typeof QuestionsListComponent;
}|{title:string;component:…'
不可分配给类型'{
标题:字符串;组件:BaseListComponent的类型;}[]”
摘录:
abstract class BaseListComponent {
protected title
}
class WeatherListComponent extends BaseListComponent {}
class QuestionsListComponent extends BaseListComponent {}
let containerItems: Array<{title: string, component: typeof BaseListComponent}>;
containerItems = [
{title:'TypeScript', component: QuestionsListComponent},
{title:'Angular2', component: QuestionsListComponent},
{title:'Weather', component: WeatherListComponent}
]
抽象类BaseListComponent{
受保护的所有权
}
类WeatherListComponent扩展了BaseListComponent{}
类QuestionsListComponent扩展BaseListComponent{}
let containerItems:数组;
containerItems=[
{标题:'TypeScript',组件:QuestionsListComponent},
{标题:'Angular2',成分:QuestionsListComponent},
{标题:'Weather',组件:WeatherListComponent}
]
p.S这是一个角度应用程序的简化摘录,因此这种疯狂背后有逻辑。我可能错了,但不是component:typeof BaseListComponent
声明component
属于类型
不应该只是组件:BaseListComponent
使用angular的界面是解决方案。可以找到关于类型的更好解释。解决步骤:
导入Type
类似于这样:Import{Type}来自“@angular/core”
将BaseListComponent的类型替换为类型
对于不使用angular的人来说,可能会有所帮助。BaseListContainerComponent
与您的示例中的BaseListComponent
不同。您的示例在中对我很好。很抱歉,我简化了名称,但在错误消息中也忘记了这么做。很奇怪,它在中不起作用一个有角度的环境,但它确实在操场上…在您的错误消息中,您试图将一个'OR'类型({}{}{}}{/code>)分配给一个数组({…}[]
),谢谢您的回答:它会导致一个错误:类型'({title:string;component:typeofquestionsContainerComponent;}){title:string;组件:…'不能分配给类型“{title:string;组件:BaseListContainerComponent;}[]”。