如何将角度组件传递给 Nebular 对话服务

Muh*_*eef 7 dialog typescript angular6 ngx-admin nebular

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

  openAddDialog = (dialogTemplate: TemplateRef<any>) => {
    this.dialogServiceRef = this.dialogService.open(dialogTemplate);
  }
Run Code Online (Sandbox Code Playgroud)

它运行良好。但我需要的是将组件作为参数传递,例如:

dialogService.open(MyComponent);
Run Code Online (Sandbox Code Playgroud)

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

|_ 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
Run Code Online (Sandbox Code Playgroud)

我已经添加了ModalFormComponentdeclarationsexportsentryComponents共享的模块中。此外,我将它导入到 SearchBar 组件中,并在单击 searchBar 中的按钮时运行此函数:

|_ 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
Run Code Online (Sandbox Code Playgroud)

我在浏览器控制台中收到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

关于问题是什么以及我该如何解决的任何想法?

mab*_*abg 5

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

例如,假设有这个 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

要传递标题和 myObject,请使用上下文参数:

const sampleObject = new MyObject();
this.dialogService.open(SimpleInputDialogComponent, {
      context: {
        title: 'Enter template name',
        myObject: sampleObject,
      },
    })
Run Code Online (Sandbox Code Playgroud)


小智 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{
}
Run Code Online (Sandbox Code Playgroud)

这应该可以解决您的错误。