Every component that will be used as dialog must be placed in declarations and in entryComponents within your app.module.ts
, as follows:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { BannerComponent } from './banner/banner.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDialogModule } from '@angular/material/dialog';
import { DialogComponent } from './dialog/dialog.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
BannerComponent,
DialogComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatDialogModule
],
providers: [],
bootstrap: [AppComponent],
entryComponents: [
DialogComponent
]
})
export class AppModule { }
I recommend you create a new module to place your Angular Material modules, as follows:
- Create a new file
.ts
in your project (can be in the same folder as app.module.ts
) and give any name, like material.module.ts
.
- Inside this file, set the basic layout:
import { NgModule } from '@angular/core';
@NgModule({
imports: [],
exports: []
})
export class MaterialModule { }
- Import the modules from the Angular Material components you want to work with, such as
MatDialogModule
.
// Para habilitar o uso de 'dialog'
import { MatDialogModule } from '@angular/material/dialog';
- Create a variable to store the reference to modules just below imports:
const materialModules = [MatDialogModule];
- Add the variable created above to
imports
and exports
in the @NgModule
:
@NgModule({
imports: [...materialModules],
exports: [...materialModules]
})
Your material.module.ts
will look like this:
import { NgModule } from '@angular/core';
// Para habilitar o uso de 'dialog'
import { MatDialogModule } from '@angular/material/dialog';
const materialModules = [MatDialogModule];
@NgModule({
imports: [...materialModules],
exports: [...materialModules]
})
export class MaterialModule { }
- Now, import into your
app.module.ts
the file you just edited and put your reference inside imports
of @NgModule
:
import { MaterialModule } from './material.module';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MaterialModule
],
declarations: [
AppComponent,
HeaderComponent,
BannerComponent,
DialogComponent
],
providers: [],
bootstrap: [AppComponent],
entryComponents: [
DialogComponent
]
})
This way, your code gets cleaner and a lot quieter to work with. Whenever you want to use a new component of Angular Material just add the module in material.module.ts
in the same way as the MatDialogModule
. For example, if you want to add the table module:
import { NgModule } from '@angular/core';
// Para habilitar o uso de 'dialog'
import { MatDialogModule } from '@angular/material/dialog';
// Para habilitar o uso de 'table'
import { MatTableModule } from '@angular/material/table';
const materialModules = [MatDialogModule, MatTableModule];
@NgModule({
imports: [...materialModules],
exports: [...materialModules]
})
export class MaterialModule { }
Hugs!
how is the code of your app.module?
– Eduardo Vargas
how can I send you the code?
– Munir Baarini
edit the question with the code
– Eduardo Vargas
put the code to the app.module in question, but it is not formatted.
– Munir Baarini