我有一个routesList
我在 Angular 的 app-routing.module 中使用
routesList
的类型是:
type routesList = {
[key: string]: {
path: string;
title: string;
component: Type<Component>;
};
};
当前导出的列表是:
export const routesList: routesList = {
home: {
path: '',
title: 'Curation Console',
component: AppComponent,
},
otherpage: {
path: 'otherpage',
title: 'Other Page',
component: OtherPageComponent,
},
};
但我收到 component: AppComponent
的错误说
typescript: Type of 'typeof AppComponent' is not assignable to type 'Type<Component>'.
Type 'AppComponent' has no properties in common with type 'Component'.
是 Type<Component>
此用途不正确?
还有其他方法可以获取 Angular 组件的类型吗?
最佳答案
Robby 详细介绍了如何/为何 Type<any>
就能解决问题。这是另一种方法。
而是定义您自己的类型,类似于 Route
(@ Angular/路由器)类型。使用现有的Route
界面。
type routesList = {
[key: string]: Route
};
type routerList = Record<string, Route>;
关于 Angular 型组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75680342/