类型X是2个模块的声明的一部分吗(Angular 2 RC5)?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (92)

我正在使用Angular 2 RC5,并遇到以下问题:

"Error: Type WelcomeComponent is part of the declarations of 2 modules: WelcomeModule and AppModule!”

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Routes, RouterModule }  from '@angular/router';
import { WelcomeModule } from './welcome/welcome.module';
import { AppComponent }  from './app.component';
import { 
    routing, 
    appRoutingProviders 
} from './app.routing';

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

app.routing.ts

import { 
    Routes,
    RouterModule 
} from '@angular/router';

import { welcomeRoutes } from './welcome/welcome.routing';

const appRoutes: Routes = [
     ...welcomeRoutes
];

export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(appRoutes);

welcome.module.ts

import { NgModule } from '@angular/core';

import { WelcomeComponent } from './welcome.component';

@NgModule({
    declarations: [ WelcomeComponent ]
})
export class WelcomeModule { }

welcome.routing.ts

import { 
    Routes,
    RouterModule 
} from '@angular/router';

import { WelcomeComponent } from './welcome.component';

export const welcomeRoutes: Routes = [
    { path: 'welcome', component: WelcomeComponent }
];

export const welcomeRoutingProviders: any[] = [];
export const welcomeRouting = RouterModule.forChild(welcomeRoutes);

我能看到的唯一解决方案是删除

import { WelcomeModule } from './welcome/welcome.module'

但是我感觉这种方法不好,有其他的解决方法吗?

提问于
用户回答回答于
用户回答回答于

下面是简化的代码示例:

@NgModule({
    imports: [CommonModule],
    declarations: [
        WelcomeComponent
    ],
    exports: [
        WelcomeComponent
    ],
    providers: [
    ]
})

扫码关注云+社区

领取腾讯云代金券