【问题标题】:Component is part of declaration of both the modules:AppRoutingModule and AppModule组件是两个模块声明的一部分:AppRoutingModule 和 AppModule
【发布时间】:2017-07-13 17:44:37
【问题描述】:

我试图通过在自己的打字稿文件中定义我的路由模块与另一个模块来分离它。但我收到上述错误:组件是两个模块声明的一部分:AppRoutingModule 和 AppModule

分享以下两个模块:

AppRoutingModule

import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'
import { AdminHomeComponent } from './nav/adminhome.component'
import { UserHomeComponent } from './nav/userhome.component'
import { ContactComponent } from './nav/contact.component'
import { LandingComponent } from './nav/mainhome.component'
import { LoginFormComponent } from './nav/login.component'


const appRoutes: Routes = [
    { path: 'login', component: LoginFormComponent },
    { path: 'adminHome', component: AdminHomeComponent },
    { path: 'userHome', component: UserHomeComponent },
    { path: 'contact', component: ContactComponent },
    { path: '', component: LandingComponent }
];


@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes)
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule { }

AppModule

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HttpModule } from '@angular/http';
import { AppRoutingModule} from './app.routing'

import { AdminHomeComponent } from './nav/adminhome.component'
import { UserHomeComponent } from './nav/userhome.component'
import { ContactComponent } from './nav/contact.component'
import { LandingComponent } from './nav/mainhome.component'
import { LoginFormComponent } from './nav/login.component'
import { ShareService } from './nav/ShareService'
//import { PaginationModule } from 'ng2-bootstrap';
//import { Ng2PaginationModule } from 'ng2-pagination';

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, AppRoutingModule ],
    declarations: [AppComponent, AdminHomeComponent, UserHomeComponent, ContactComponent, LandingComponent, LoginFormComponent],
    bootstrap: [AppComponent],
    providers: [ShareService]

})
export class AppModule { }

我关注了https://angular.io/docs/ts/latest/guide/router.html 路由文档,但遇到了这样的错误。

谁能看看代码中是否存在一些错误。谢谢。

【问题讨论】:

  • 错误信息是否说明了两个模块的组成部分是什么?
  • 是的。 AdminHomeComponent。
  • 这可以在 plunk 中重现吗?

标签: angular module routing components


【解决方案1】:

我认为您应该将AdminHomeComponentAppRoutingModule 中引用的所有其他组件从AppModule 移出到不同的模块中,并将此模块添加到importsAppRoutingModule 中。

【讨论】:

  • 但是我的实现有什么问题。它是根据角度文档。再定义一个模块只是为了再次定义组件将是一种矫枉过正。
  • AFAIK 路由器添加的组件会自动添加到路由中使用它们的模块的entryComponentsdeclarations,当在导入的模块中找不到时。我不知道文档(并不意味着没有),因为组件不能是多个模块的一部分,这会导致错误。
  • 但是你检查过文档吗?为什么不提这种事?
  • 不,我没有检查文档。有很多关于路由器和 NgModule 的文档。
  • 所以你的意思是我应该创建一个新的module 并在其中导入我所有的components。之后我应该只使用export 并在AppRoutingModuleAppModule 中使用它。这是你的意思吗?
【解决方案2】:

我不能发表评论,我会写这个作为答案。

这很奇怪,代码应该可以工作。我尝试了相同的逻辑,它对我有用。

您能否尝试从appRoutes 中删除AdminHomeComponent,看看它是否只是AdminHomeComponent 或其他组件也会引发同样的问题。

如果您对其他组件也有同样的问题。尝试仅在您的路由模块中声明它们,看看它是否有效(但这不是一个好习惯)。

还有另一种方法:

app-routing.module.ts

import { Host, ModuleWithProviders } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { OtherComponent } from './other.component';
import { HomeComponent } from './home.component';

const appRoutes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'other', component: OtherComponent},
  { path: '', component: HomeComponent }
];

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

app.module.ts

import {  routing } from './app-routing.module';
import { OtherComponent } from './other.component';
import { HomeComponent } from './home.component';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    OtherComponent
],
imports: [
  BrowserModule,
  FormsModule,
  HttpModule,
  routing
],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

就个人而言,我会按照其他 2 个答案中的建议进行操作。

顺便问一下,您的应用中还有其他模块吗?

【讨论】:

  • 它在每个模块中都抛出错误,因为它们都在模块中重复。您的回答也没有指向解决方案的方向,因为它还在多个模块中导入了组件,这就是问题所在。
【解决方案3】:

不可能!

您可以创建一个仅包含该组件的模块并将其导入到您的模块中。

【讨论】:

  • 1.创建新模块(例如 AdminModule) 2. 在该模块中定义 AdminHomeComponent 3. 从该模块中导出 AdminHomeComponent 4. 将 AdminModule 导入到您需要 AdminHomeComponent 的模块中
  • 这正是我所做的,正如 Gunter 在其他答案中所建议的那样。如果你看到与 Gunter 的聊天,你就会知道我只遵循了这个过程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 2020-11-13
  • 2017-09-21
  • 1970-01-01
  • 2020-05-31
  • 2019-03-28
  • 1970-01-01
相关资源
最近更新 更多