Angular5使用模块中的组件

时间:2018-04-21 16:20:19

标签: angular typescript

我是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 /应用/管理/ 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 /应用/管理/管理家庭/管理-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.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 { }

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

但是,我是否应该能够使用从模块导出的组件,只需从模块中导入该组件即可?

任何建议的Thx!

3 个答案:

答案 0 :(得分:1)

如果从模块文件导入它,则必须将其导出到admin.module.ts中

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

答案 1 :(得分:0)

您正在从错误的文件导入。

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

module替换为文件名中的component

答案 2 :(得分:0)

嗯,你忘了在模块中声明它

试试这个

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 { }