简体   繁体   English

Angular - 将mat对话框封装到模块中

[英]Angular - encapsulate a mat-dialog to a module

I have a dialog that I want to share across different parts of my app. 我有一个对话框,我想在我的应用程序的不同部分分享。 Different components in different modules should all be able to trigger this same dialog and the logic that goes with it. 不同模块中的不同组件都应该能够触发相同的对话框以及随之而来的逻辑。 The logic of interacting with the dialog is embedded within a service that also handles what to do with the user's inputs. 与对话框交互的逻辑嵌入在服务中,该服务还处理如何处理用户的输入。 The dialog is invoked by calling a method of the service. 通过调用服务的方法来调用该对话框。

This dialog component has no business being declared at the Application level. 此对话框组件未在应用程序级别声明业务。 That is poor encapsulation. 这是不好的封装。 The dialog should only be accessed by the Service, so it should only be visible TO the service. 该对话框只能由服务访问,因此只应对服务可见。 Therefore, I should declare it in the same module as the service. 因此,我应该在与服务相同的模块中声明它。

I've declared and exported the component in my module, like so: 我在我的模块中声明并导出了组件,如下所示:

@NgModule({
  imports:      [ ... ],
  declarations: [ NewItemComponent ],
  exports:      [ NewItemComponent ]
})
export class SharedServicesModule {
  static forRoot(): ModuleWithProviders {
    return { 
      ngModule: SharedServicesModule,
      providers: [ ... ]
    }
  }
}

And I referenced it in the app.module.ts , like so: 我在app.module.ts引用它,如下所示:

@NgModule({
  declarations:    [ ... ],
  imports:         [ SharedServicesModule.forRoot(), ... ],
  entryComponents: [ NewItemComponent ],
  bootstrap:       [ AppComponent ]
})

Unfortunately, that doesn't seem to be working. 不幸的是,这似乎不起作用。

ERROR Error: No component factory found for NewItemComponent. 
Did you add it to @NgModule.entryComponents?

I tried adding it to entryComponents at the module level. 我尝试在模块级别将其添加到entryComponents To both. 二者皆是。 And even to the modules that are consuming the dialog via the service. 甚至通过服务消耗对话的模块。 To no avail. 无济于事。

What is the right way to make this work? 使这项工作的正确方法是什么?

Or have I run into this: https://github.com/angular/angular/issues/14324 或者我遇到过这个问题: https//github.com/angular/angular/issues/14324

I don't know if it is the ideal solution, but I found a solution. 我不知道它是否是理想的解决方案,但我找到了解决方案。 I don't understand why this works, and what I tried earlier didn't, but I have working code, so I'm going to leave it alone... 我不明白为什么这样可行,而我之前尝试过的却没有,但我有工作代码,所以我会不管它...

I created a stand-alone module to hold the dialog component, which includes the entryComponents entry, like so: 我创建了一个独立的模块来保存对话框组件,其中包括entryComponents条目,如下所示:

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    MaterialDesignModule
  ],
  declarations:    [ NewItemDialogComponent ],
  exports:         [ NewItemDialogComponent ],
  entryComponents: [ NewItemDialogComponent ]  // <-- NOTICE!
})
export class NewItemDialogModule { }

Then I import that module into the module that is going to use it: 然后我将该模块导入到将要使用它的模块中:

@NgModule({
  imports: [
    CommonModule,
    NewItemDialogModule   // <-- HERE
  ]
})
export class SharedServicesModule { 
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedServicesModule,
      providers: [ ... ]      
    };
  }
}

I did NOT have to import the dialog into the app.module , which was the goal. 我没有必要将对话框导入app.module ,这是目标。

If anybody knows why it didn't work when everything was directly in the SharedServicesModule , do tell. 如果有人知道为什么它直接在SharedServicesModule它不起作用,请告诉。

Your NewItemComponent is under SharedServicesModule , 您的NewItemComponent位于SharedServicesModule下,

But you are doing entryComponents: [ NewItemComponent ] in app.module.ts. 但是你正在entryComponents: [ NewItemComponent ] here is the problem. 这是问题所在。 You should do it in SharedServicesModule not in AppModule . 您应该在SharedServicesModule而不是在AppModule执行此AppModule

Add entryComponents: [ NewItemComponent ] in SharedServicesModule , so the code is SharedServicesModule添加entryComponents: [ NewItemComponent ] ,所以代码是

@NgModule({
  imports:      [ ... ],
  declarations: [ NewItemComponent ],
  exports:      [ NewItemComponent ],
  entryComponents: [ NewItemComponent ] // it is here in the **SharedServicesModule** 
})

export class SharedServicesModule {
  static forRoot(): ModuleWithProviders {
    return { 
      ngModule: SharedServicesModule,
      providers: [ ... ]
    }
  }
}

Remove entryComponents: [ NewItemComponent ] from app.module.ts as this component is not under this module. entryComponents: [ NewItemComponent ]删除entryComponents: [ NewItemComponent ] ,因为此组件不在此模块下。

@NgModule({
  declarations:    [ ... ],
  imports:         [ SharedServicesModule.forRoot(), ... ],
  bootstrap:       [ AppComponent ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM