首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 8 ngx-bootstrap模式未找到组件工厂

Angular 8 ngx-bootstrap模式未找到组件工厂
EN

Stack Overflow用户
提问于 2019-07-10 17:21:50
回答 2查看 4.1K关注 0票数 5

我有一个模块,看起来像这样:

代码语言:javascript
复制
@NgModule({
  imports: [
    EmployeeRoutingModule,
  ],
  declarations: [
    ListEmployeeComponent,
    EmployeeDialogComponent,
  ],
  providers: [
    EmployeeService,
    BsModalService,
  ],
  entryComponents: [
    EmployeeDialogComponent,
  ]
})
export class EmployeeModule {
}

然后导入到app模块中,如下所示

代码语言:javascript
复制
{
  path: 'employee',
  loadChildren: './employee/employee.module#EmployeeModule'
},

app.routing.ts中。

当我尝试在我的EmployeeModule中使用EmployeeDialogComponent时,它一直给出错误

代码语言:javascript
复制
ERROR Error: No component factory found for EmployeeDialogComponent. Did you add it to @NgModule.entryComponents?

如果我将它添加到一个模块(让我们称它为SharedModule),就像在应用程序运行时加载(在app.module.ts文件中)一样,它就能工作。这似乎是因为懒惰地使用它。

问题是什么?我如何解决它?

下面是一个无法工作的模式的最小版本的示例:

代码语言:javascript
复制
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {BsModalRef} from 'ngx-bootstrap';

@Component({
  selector: 'modal-content',
  template: `
    <div class="modal-body text-center">
      <p class="lead mb-5">{{ message }}</p>
      <button type="button" class="btn btn-lg btn-outline-secondary mr-5" (click)="decline()" >{{ btnNoText }}</button>
      <button type="button" class="btn btn-lg btn-success" (click)="confirm()" >{{ btnYesText }}</button>
    </div>
  `
})
export class ConfirmEmployeeModalComponent implements OnInit {
  @Output() result = new EventEmitter<boolean>();

  message = 'Are you sure?';
  btnNoText = 'No';
  btnYesText = 'Yes';

  constructor(public bsModalRef: BsModalRef) {
  }

  ngOnInit() {}

  confirm(): void {
    this.result.emit(true);
    this.bsModalRef.hide();
  }

  decline(): void {
    this.result.emit(false);
    this.bsModalRef.hide();
  }
}

我在模块ListEmployeeComponent中有一个组件,它包含如下函数:

代码语言:javascript
复制
import {
  Component,
  NgZone,
  ElementRef,
  OnInit,
  ViewContainerRef,
  PipeTransform,
  Pipe,
  ViewChild
} from '@angular/core';
import {FormControl} from '@angular/forms';
import {DialogService, BuiltInOptions} from "ngx-bootstrap-modal";
//import { EmployeeDialogComponent } from '../employee-dialog/employee-dialog.component';
import {EmployeeService} from "../employee.service";
import {LocalStorageUtilityService, NotificationService} from "../../common/common.services";
import * as _ from 'lodash';
//import { NotifyDialogComponent } from '../../common/dialog/notify-dialog/notify-dialog.component';
import {UserService} from "../../common/user.service";
import {ConfirmModalComponent} from "../../common/confirm-modal/confirm-modal.component";
import {BsModalService} from "ngx-bootstrap/modal";
import {EmployeeDialogComponent} from "../employee-dialog/employee-dialog.component";
import {ConfirmEmployeeModalComponent} from "../confirm-employee-modal/confirm-modal.component";

@Component({
  templateUrl: 'list-employee.component.html',
  styleUrls: ['./list-employee.component.css']
})
export class ListEmployeeComponent {
  constructor(
    private modalService: BsModalService,
    public _dialogService: DialogService,
    public _companyService: EmployeeService,
    public _notificationService: NotificationService,
    private userService: UserService,
  ) {
  }

  showAddEmployeeDialog = () => {
    this.modalService.show(ConfirmEmployeeModalComponent, {}).content.result.subscribe((details: any) => {
      }
    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-10 18:34:43

我设法在this repo中重现了您的问题,将ModalModule.forRoot()添加到EmployeeModule导入将修复该错误。

我在使用MatDialog时遇到了类似的问题,当我想要在已经创建的MatDialog实例(这是一个可注入的)中使用来自延迟加载模块的组件时,我得到了相同的错误。然后我注意到,当使用延迟加载的组件时,应该从头开始初始化服务。在你的案例中也发生了类似的事情。

根据docsModalModule是用forRoot() like导入的;

代码语言:javascript
复制
// RECOMMENDED
import { ModalModule } from 'ngx-bootstrap/modal';
// or
import { ModalModule } from 'ngx-bootstrap';

@NgModule({
  imports: [ModalModule.forRoot(),...]
})
export class AppModule(){}

这意味着BsModalService的全局单例实例被创建并在整个应用程序中使用。而且它在初始化时对延迟加载的组件没有任何了解。

因此,解决方案是为延迟加载的模块初始化一个新实例,以便向EmployeeModule导入添加ModalModule.forRoot()可以修复错误。

代码语言:javascript
复制
@NgModule({
  imports: [
    EmployeeRoutingModule,
    ModalModule.forRoot()
  ],
  declarations: [
    EmployeeDialogComponent,
    ListEmployeeComponent,
  ],
  providers: [
    BsModalService,
  ],
  entryComponents: [
    EmployeeDialogComponent,
  ]
})
export class EmployeeModule {}

但是请注意,这将创建一个专用于EmployeeModule的新BsModalService实例,它不会知道它之外的任何其他上下文,也不会知道任何外部上下文不知道它。简单地说,这意味着除了EmployeeModule context之外,您不能在任何其他地方与此特定对话框交互(例如关闭它)。

票数 8
EN

Stack Overflow用户

发布于 2019-09-11 06:15:36

在我的例子中,解决方案是在模块中添加以下内容:

代码语言:javascript
复制
import {  NgbModule } from '@ng-bootstrap/ng-bootstrap';
imports: [
    NgbModule
]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56967488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档