RC6升级时子路径错误

时间:2016-09-08 19:48:54

标签: angular2-routing

升级到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);

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

编辑:您正在从

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

如果您使用AppComponent中的相同约定(您应该使用),则应从

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

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

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

相关问题