【问题标题】:Child routes error with RC6 upgradeRC6 升级的子路由错误
【发布时间】:2016-09-08 19:48:53
【问题描述】:

升级到 RC6 时,我的路由出现此运行时错误(这包括子路由):

ListingComponent 不是任何 NgModule 的一部分,或者该模块没有 已导入到您的模块中

此错误表明我没有将 ListingComponent 添加到 ngModule 但它作为声明存在。

在 app.module 中,我将所有组件作为声明。我还导入了我的路由组件。路由组件有一个子路由组件,叫做listing.routes。

这是我的 app.module.ts:

import {NgModule, CUSTOM_ELEMENTS_SCHEMA, ReflectiveInjector  } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule, FormBuilder} from '@angular/forms';
import { NgClass, NgStyle} from '@angular/common';
import { AppComponent }   from './app.component';
import {routing} from './app.routes';
import {ListingModule} from './components/listing/listingmodule';
import {ListingComponent} from './components/listing/listing.Component';

@NgModule({
    imports: [BrowserModule, routing],
    providers: [],
    declarations: [AppComponent, ListingComponent, ListingModule],
    bootstrap: [AppComponent]
})
export class AppModule {

}

这是我的 app.routes.ts(我将其作为路由导入):

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {ListingRoutes} from './components/listing/listing.routes';
import {SplashComponent} from './components/splash/splash.component';

export const appRoutingProviders: Routes = ([
    { path: '', component: SplashComponent },
    { path: 'login', component: SplashComponent },
    ...ListingRoutes
]);

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutingProviders);

这是我的listing.routes.ts:

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {ListingModule} from './repairreturnmodule';
import {ListingComponent} from '../listing/listing.component';


export const ListingRoutes: Routes = [
    {
        path: '',
        component: ListingModule,
        children: [
            { path: 'listing', component: ListingComponent},
        ]
    }
];

export const ListingRouting: ModuleWithProviders = RouterModule.forChild(ListingRoutes);

我错过了什么吗?

【问题讨论】:

    标签: angular2-routing


    【解决方案1】:

    编辑:您正在从

    导入您的 ListingComponent
    import {ListingComponent} from './components/listing/listing.Component';
    

    如果你使用与 AppComponent 相同的约定(你应该这样做),你应该从

    导入
    import {ListingComponent} from './components/listing/listing.component';
    

    查看文件名。其余代码看起来正确。


    我不确定这是否是问题的根源,但是您在 AppModule 中声明了一个模块,当您应该导入它时... 尝试将 ListingModule 从声明数组移动到导入数组。

    【讨论】:

    • 好的。抱歉,谢谢。我做了一个可能有用的编辑。这样更好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2014-04-28
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多