我正在使用一些简单的路由创建一个Angular 2应用程序:
(main.module.ts)
import { routes } from './main.routes';
/* Other imports */
@NgModule({
imports: [
HomeModule,
TestModule,
RouterModule.forRoot(routes) // imported from file below
],
declarations: [MainComponent, SidebarComponent],
exports: [MainComponent, SidebarComponent]
})
(main.routes.ts)
import { Route } from '@angular/router';
/* Other imports */
export const routes = [
{
path: '',
component: MainComponent,
children: [
{ path: 'home', component: HomeComponent },
{ path: 'test', component: TestComponent }
]
}
];
当我键入npm start时,应用程序运行正常但我的IntelliJ IDE在RouterModule.forRoot(routes)
参数类型{path:string,component:MainComponent,children:{path:string,component:HomeComponent} | {path:string,component:TestComponent} []} []不能分配给参数Routes
export const routes: Routes = ]
而不是 export const routes = [
搜索完毕后,我无法找到理由或替代方案,无误地实施。
答案 0 :(得分:1)
使用:
解决了这个问题export const routes: Routes = [
{ path: '', component: <any>MainComponent },
{ path: '', component: <any>MainComponent }
];
我通过查看Route []的定义找到了它。结束发现冲突只发生在component: MainComponent
。发现界面Route
期望它为component?: Type<any>;
。在乞讨时我虽然不是类型问题(不重复)。但是,我总结说,预计会扩展Type 。
致谢:here并感谢@BeetleJuice