如何将 Angular 组件传递给 Nebular 对话框服务

7

我正在尝试使用Nebular组件在Angular6上创建Web对话框。有两种方法可以实现这一点,第一种是传递<ng-template>引用,例如:

  openAddDialog = (dialogTemplate: TemplateRef<any>) => {
    this.dialogServiceRef = this.dialogService.open(dialogTemplate);
  }

它现在运行良好。但我需要像这样将组件作为参数传递:

dialogService.open(MyComponent);

我在我的应用程序中有这样的结构:

|_ pages
|   |_ pages.module.ts
|   |_ patient
|      |_ patient.module.ts
|      |_ patient.component.ts
|      |_ patient.component.html
|
|_ shared
    |_ shared.module.ts
    |_ components
    |   |_ search-bar.component.ts
    |   |_ search-bar.component.html
    |
    |_ modal-form
        |_ modal-form.component.ts
        |_ modal-from.component.html

我已将 ModalFormComponent 添加到共享模块的 declarationsexportsentryComponents 中。同时,我在 SearchBar 组件中导入,并在点击 searchBar 中的按钮时运行此函数:
openAddDialog = () => {
    this.dialogServiceRef = this.dialogService.open(ModalFormComponent);
  }

我在浏览器控制台中看到了这个错误:

ERROR Error: No component factory found for ModalFormComponent. Did you add it to @NgModule.entryComponents?
    at noComponentFactoryError (core.js:19453)
    at CodegenComponentFactoryResolver.resolveComponentFactory (core.js:19504)
    at NbPortalOutletDirective.attachComponentPortal (portal.js:506)
    at NbDialogContainerComponent.attachComponentPortal (index.js:17128)
    at NbDialogService.createContent (index.js:17336)
    at NbDialogService.open (index.js:17295)
    at SearchBarComponent.openAddDialog (search-bar.component.ts:46)
    at Object.eval [as handleEvent] (SearchBarComponent.html:11)
    at handleEvent (core.js:34777)
    at callWithDebugContext (core.js:36395)

您认为问题出在哪里,以及我该如何解决它?

4个回答

6

使用上下文参数传递所有所需参数。

例如,假设您有这个SimpleInputDialogComponent:

import { Component, Input } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';

@Component({
  selector: 'ngx-simple-input-dialog',
  templateUrl: 'simple-input-dialog.component.html',
  styleUrls: ['simple-input-dialog.component.scss'],
})
export class SimpleInputDialogComponent {

  title: String;
  myObject: MyObject;

  constructor(protected ref: NbDialogRef<SimpleInputDialogComponent>) {
  }

  cancel() {
    this.ref.close();
  }

  submit(value: String) {
    this.ref.close(value);
  }
}

使用上下文参数传递标题和myObject:
const sampleObject = new MyObject();
this.dialogService.open(SimpleInputDialogComponent, {
      context: {
        title: 'Enter template name',
        myObject: sampleObject,
      },
    })

感谢回答@mabg 但问题不在于如何发送组件输入。问题在于发送组件方法根本不起作用! - Muhammad Al-Shareef

1
这是nebular文档所建议的:

nebular

open() {
 this.dialogService.open(yourComponent, {
   context: {
     title: 'This is a title passed to the dialog component',
   },
 });
}

1
尝试将组件放置在模块的entryComponents字段中(无论是主模块还是子模块,取决于您的情况)。
import {NgModule} from '@angular/core';

import {
  //...
  NbDialogModule
  //...
} from '@nebular/theme';


@NgModule({
 declarations: [
 //...,
 ModalFormComponent,
 //...
 ],
 entryComponents: [
  //...
  ModalFormComponent,
  //...
 ],
 imports: [
 //...
 // NbDialogModule.forChild() or NbDialogModule.forRoot()
 //...
],
 providers: [
  //...
 ],
})
 export class ExampleModule{
}

这应该解决你的错误。

谢谢你的回答!你完全正确,已经解决了这个错误! - Muhammad Al-Shareef

0
解决方案:应该在ngOnInit中获取参数,而不是在构造函数中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接