在Angular中,我正在尝试合并两个数组,两者都匹配基本接口,但不完全相同:
export const orgAdminRoutes: Route[] = [
{
path: 'admin',
component: AdminComponent,
resolve: {
org: OrgResolver
},
canActivate: [
AdminGuard
],
children: [
{
path: 'users',
component: UsersComponent
}
]
},
];
let orgChildren: Route[] = [
{
path: 'events',
component: EventsListComponent,
},
];
我想加入这两个数组,但希望只将orgChildren
作为最终变量,所以尝试了:
let orgChildren: Route[] = [
{
path: 'events',
component: EventsListComponent,
},
].concat(orgAdminRoutes);
这引发了这个错误:
'Argument of type 'Route[]' is not assignable to parameter of type '{ path: string; component: typeof EventsListComponent; } | { path: string; component: typeof Even...'.
Type 'Route[]' is not assignable to type '{ path: string; component: typeof EventsListComponent; }[]'.
Type 'Route' is not assignable to type '{ path: string; component: typeof EventsListComponent; }'.
Property 'path' is optional in type 'Route' but required in type '{ path: string; component: typeof EventsListComponent; }'.'
如果我只是单独创建两个,然后在代码之后(orgChildren.concat(orgAdminRoutes)
)之后连接它们,它工作正常,但我不明白为什么,并希望有人可能知道?
答案 0 :(得分:0)
所以我可能错了,但我认为正在发生的事情是
let orgChildren: Route[] = [
{
path: 'events',
component: EventsListComponent,
},
].concat(orgAdminRoutes);
我的假设是我正在定义的数组将被输入为Route[]
,但这是在分配后发生的。因此,当我连接时,我正在使用其中的类型对数组进行查询,这与Route[]
类型不匹配。我想我可以做一些有趣的类型转换技巧让它适合一行,但也许两个更好。