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

前端之家收集整理的这篇文章主要介绍了类型X是2个模块的声明的一部分(角2 RC5)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我和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);

我可以看到Angular正在抱怨,welcomeRoutes要求WelcomeModule中的WelcomeComponent被定义,appRoutes引用welcomeRoutes和路由是在AppModule中导入的,它有WelcomeModuleimported,但是我应该如何构建跨多个模块的路由图呢?

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

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

但是那种那样会把模块放在第一位的整个目的呢,不是吗?

您为root和child加载相同的路由配置,但它们必须是不同的路由配置.有关完整示例,请参阅 https://angular.io/docs/ts/latest/guide/router.html
原文链接:https://www.f2er.com/angularjs/142584.html

猜你在找的Angularjs相关文章