【问题标题】:Angular5 Use Component from ModuleAngular5使用模块中的组件
【发布时间】:2018-04-21 16:20:19
【问题描述】:

我是 A5 的菜鸟,正在做我的第一个“将组件重构为模块”。我正在尝试使用在另一个模块的一个模块中定义的组件。我得到了错误,ERROR in src/app/app-routing.module.ts(9,10): error TS2305: Module '"xxx/src/app/admin/admin.module"' has no exported member 'AdminHomeComponent'.

我查看了其他相关问题,但我只是缺少一些东西。希望你能帮忙。

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home/home.component';
import { ArchitectureComponent } from './architecture/architecture.component';
import { DesignComponent } from './design/design.component';
import { HistoryComponent } from './history/history.component';
import { AdminHomeComponent } from './admin/admin.module';

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'architecture', component: ArchitectureComponent },
  { path: 'design', component: DesignComponent },
  { path: 'history', component: HistoryComponent }
];

@NgModule({
  exports: [ RouterModule ],
  imports: [ RouterModule.forRoot(routes)]
})

export class AppRoutingModule { }

src/app/admin/admin.module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminHomeComponent } from './admin-home/admin-home.component';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    AdminHomeComponent
  ],
  declarations: [
    AdminHomeComponent  // <-- Tried adding this based on comments, no help
  ]
})
export class AdminModule { }

src/app/admin/admin-home/admin-home.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-admin-home',
  templateUrl: './admin-home.component.html',
  styleUrls: ['./admin-home.component.scss']
})
export class AdminHomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

如果有帮助的话,

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { MDBBootstrapModule } from 'angular-bootstrap-md';
import { TreeModule } from 'angular-tree-component';

import { AppComponent } from './app.component';
...
import { HomeComponent } from './home/home.component';
import { ComponentsModule } from './components/components.module';
import { AdminModule } from './admin/admin.module';

@NgModule({
  declarations: [
    AppComponent,
    ...
    HomeComponent,
    AdminModule   // <-- Tried adding this based on comments, but no help
  ],
  imports: [
    BrowserModule,
    MDBBootstrapModule.forRoot(),
    TreeModule,
    AppRoutingModule,
    AdminModule,
    ComponentsModule
  ],
  schemas: [ NO_ERRORS_SCHEMA ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

也许答案很简单:直接从模块中导入组件?

但是我不应该只通过从模块中导入该组件来使用从模块中导出的组件吗?

感谢您的任何建议!

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    如果你从模块文件中导入它,你必须在你的 admin.module.ts 中以这种方式导出它

      import { AdminHomeComponent } from './admin-home/admin-home.component';
      export {AdminHomeComponent };
    

    【讨论】:

    • 谢谢,法塔赫!这成功了。但这让我很困惑。你能提供一个关于export的文档的指针吗?我没有在任何例子中看到这一点。谢谢!
    • 在您的 admin.module 中,您没有任何名为 AdminHomeComponent 的导出类,因此您必须以这种方式导出它,然后才能导入它并将其用作组件,但最好的方式是您从它的 ts 文件“admin-home.component.ts”中导入共享组件
    • 但我建议您创建一个 sharedModule 并将所有共享的内容(组件、服务、管道、指令......),然后在需要时将其导入另一个模块,检查此 angular-2-training-book.rangle.io/handout/modules/…
    • 谢谢,法塔赫!我很欣赏这些示例,但同样,它们不包括模块导出子组件的模块的子组件。所以他们不导出 { XxxComponent };正如您所指出的,这是我的问题......应该如何使用共享模块中的组件?该模块应该如何导出组件?
    • 这只有在你从模块文件中导入组件时才有效,但最好是从它的文件中导入它:)
    【解决方案2】:

    您从错误的文件导入。

    import { AdminHomeComponent } from './admin/admin.module';
    

    将文件名中的module 替换为component

    【讨论】:

    • 谢谢,Lazar,但没有 admin.component。这是一个模块。
    • 这正是您遇到的问题,但没关系。
    • 谢谢,拉扎尔,但是......我不(现在仍然不)明白如何将你所说的翻译成我的问题的解决方案。 @Fateh 的回答说我应该添加一个export {}。这解决了我的问题。但我仍然不确定这是最好的方法。你能详细说明一下吗?
    【解决方案3】:

    你忘了在模块中声明它

    试试这个

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { AdminHomeComponent } from './admin-home/admin-home.component';
    
    @NgModule({
      imports: [
        CommonModule
      ],
      exports: [
        AdminHomeComponent
      ],
      declarations: [AdminHomeComponent]
    })
    export class AdminModule { }
    

    我还发现在 app-routing.module.ts: 你有 import { AdminHomeComponent } from './admin/admin.module'; 删除它。

    而是在 src/app/admin/admin.module 中这样做

    import { NgModule } from '@angular/core';
    import { RouterModule} from '@angular/router';
    import { CommonModule } from '@angular/common';
    import { AdminHomeComponent } from './admin-home/admin-home.component';
    
    @NgModule({
      imports: [
        CommonModule,
        RouterModule.forRoot([
        { path: 'admin', component: AdminHomeComponent } 
      ])
      ],
      exports: [
        AdminHomeComponent
      ],
      declarations: [AdminHomeComponent]
    })
    export class AdminModule { }
    

    【讨论】:

    • 谢谢,Amaya,但这并没有帮助。将其声明为出口不就足够了吗?
    • 是的,不是因为每个组件都需要声明。
    • Thx, Amaya, ... 描述的这个组件是否需要声明?到目前为止,我的尝试似乎并不重要。
    • 尝试重建你的应用程序
    • 错误来自ng build
    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 2018-12-02
    • 2021-10-27
    • 2017-06-27
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多