加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 在typescript中定义typeof抽象类

发布时间:2020-12-17 17:33:18 所属栏目:安全 来源:网络整理
导读:如何将typeList BaseListComponent设置为可分配? 下面的代码引发以下错误: Type ‘ ({ title: string; component: typeof QuestionsListComponent; } | { title: string; component: ...' is not assignable to type '{ title: string; component: typeof
如何将typeList BaseListComponent设置为可分配?

下面的代码引发以下错误:

Type ‘({ title: string; component: typeof QuestionsListComponent;
} | { title: string; component: ...'
is not assignable to type '{
title: string; component: typeof 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',{title:'Weather',component: WeatherListComponent}
]

P.S这是一个角度应用的简化摘录,所以这种疯狂背后有逻辑.

解决方法

使用angular的 Type接口是解决方案.可以找到更好的解释什么类型是 here.要解决的步骤:

>导入类型如下:import @ Type} from“@ angular / core”;
>将Typeof BaseListComponent替换为Type< BaseListComponent>

对于那些不使用角度的人来说,this thread可能会有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读