Angular2 material dialog has issues - Did you add it to @NgModule.entryComponents?

前端 未结 11 2118
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 06:37

I am trying to follow the docs on https://material.angular.io/components/component/dialog but I cannot understand why it has the below issue?

I added the below on my

相关标签:
11条回答
  • 2020-12-04 06:40

    I had the same issues and i had dialogComponent in EntryComponents and it still did not work. this is how I was able to solve the problem. the link is here to a previously answered post:

    https://stackoverflow.com/a/64224799/14385758

    0 讨论(0)
  • 2020-12-04 06:43

    If you're trying to use MatDialog inside a service - let's call it 'PopupService' and that service is defined in a module with:

    @Injectable({ providedIn: 'root' })
    

    then it may not work. I am using lazy loading, but not sure if that's relevant or not.

    You have to:

    • Provide your PopupService directly to the component that opens your dialog - using [ provide: PopupService ]. This allows it to use (with DI) the MatDialog instance in the component. I think the component calling open needs to be in the same module as the dialog component in this instance.
    • Move the dialog component up to your app.module (as some other answers have said)
    • Pass a reference for matDialog when you call your service.

    Excuse my jumbled answer, the point being it's the providedIn: 'root' that is breaking things because MatDialog needs to piggy-back off a component.

    0 讨论(0)
  • 2020-12-04 06:44

    If you are like me, and are staring at this thread thinking "But I'm not trying to add a component, I am trying to add a guard/service/pipe, etc." then the issue is likely that you have added the wrong type to a routing path. That is what I did. I accidentally added a guard to the component: section of a path instead of the canActivate: section. I love IDE autocomplete but you have to slow down a bit and pay attention. If you absolutely can't find it, do a global search for the name it is complaining about and look at every usage to make sure you didn't slip-up with a name.

    0 讨论(0)
  • 2020-12-04 06:46

    In case of lazy loading, you just need to import MatDialogModule in lazy loaded module. Then this module will be able to render entry component with its own imported MatDialogModule:

    @NgModule({
      imports:[
        MatDialogModule
      ],
      declarations: [
        AppComponent,
        LoginComponent,
        DashboardComponent,
        HomeComponent,
        DialogResultExampleDialog        
      ],
      entryComponents: [DialogResultExampleDialog]
    
    0 讨论(0)
  • 2020-12-04 06:48

    You need to use entryComponents under @NgModule.

    This is for dynamically added components that are added using ViewContainerRef.createComponent(). Adding them to entryComponents tells the offline template compiler to compile them and create factories for them.

    The components registered in route configurations are added automatically to entryComponents as well because router-outlet also uses ViewContainerRef.createComponent() to add routed components to the DOM.

    So your code will be like

    @NgModule({
      declarations: [
        AppComponent,
        LoginComponent,
        DashboardComponent,
        HomeComponent,
        DialogResultExampleDialog        
      ],
      entryComponents: [DialogResultExampleDialog]
    
    0 讨论(0)
  • 2020-12-04 06:55

    This is happening because this is a dynamic component and you didn't add it to entryComponents under @NgModule.

    Simply add it there:

    @NgModule({
      /* ----------------- */
      entryComponents: [ DialogResultExampleDialog ] // <---- Add it here
    

    Look at how the Angular team talks about entryComponents:

    entryComponents?: Array<Type<any>|any[]>

    Specifies a list of components that should be compiled when this module is defined. For each component listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.

    Also, this is the list of the methods on @NgModule including entryComponents...

    As you can see, all of them are optional (look at the question marks), including entryComponents which accept an array of components:

    @NgModule({ 
      providers?: Provider[]
      declarations?: Array<Type<any>|any[]>
      imports?: Array<Type<any>|ModuleWithProviders|any[]>
      exports?: Array<Type<any>|any[]>
      entryComponents?: Array<Type<any>|any[]>
      bootstrap?: Array<Type<any>|any[]>
      schemas?: Array<SchemaMetadata|any[]>
      id?: string
    })
    
    0 讨论(0)
提交回复
热议问题