【问题标题】:Angular 2 Router Module function gives error "Argument type ... is not assignable to parameter type Routes"Angular 2 路由器模块功能给出错误“参数类型......不可分配给参数类型路由”
【发布时间】:2017-03-04 23:17:14
【问题描述】:

我正在创建一个带有一些简单路由的 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 = [

经过搜索,我无法找到没有错误地实施它的原因或替代方案。

【问题讨论】:

  • 当你使用export const routes: Routes时,如果你双击类型Routes,它应该带你到类型定义。定义是什么样的,它在哪个文件中?

标签: angular intellij-idea


【解决方案1】:

通过使用解决它:

export const routes: Routes = [
   { path: '', component: <any>MainComponent },
   { path: '', component: <any>MainComponent }
];

我是通过查看Route[]的定义找到的。最终发现冲突仅在component: MainComponent 中。碰巧接口Route 期望它是component?: Type&lt;any&gt;;。在乞求时,我虽然不是类型问题(不是重复的)。但是,我得出的结论是,它有望扩展 Type

致谢:here 并感谢 @BeetleJuice

【讨论】:

    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2022-01-24
    • 2021-09-25
    • 1970-01-01
    • 2020-02-20
    • 2021-10-03
    相关资源
    最近更新 更多