Angular 2路由器模块功能提供错误"参数类型...不能分配给参数类型路由"

时间:2016-10-22 21:07:18

标签: angular intellij-idea

我正在使用一些简单的路由创建一个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

enter image description here

我也尝试过:

export const routes: Routes = ] 而不是 export const routes = [

搜索完毕后,我无法找到理由或替代方案,无误地实施。

1 个答案:

答案 0 :(得分:1)

使用:

解决了这个问题
export const routes: Routes = [
   { path: '', component: <any>MainComponent },
   { path: '', component: <any>MainComponent }
];

我通过查看Route []的定义找到了它。结束发现冲突只发生在component: MainComponent。发现界面Route期望它为component?: Type<any>;。在乞讨时我虽然不是类型问题(不重复)。但是,我总结说,预计会扩展Type

致谢:here并感谢@BeetleJuice