I have added Angular Material module like this in material.module.ts file.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import { MatInputModule } from '@angular/material/input';
import { MatDatepickerModule } from '@angular/material/datepicker';
@NgModule({
declarations: [],
imports: [
CommonModule,
MatToolbarModule,
MatIconModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatSelectModule,
MatInputModule,
MatDatepickerModule,
],
exports: [
MatToolbarModule,
MatIconModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatSelectModule,
MatInputModule,
MatDatepickerModule,
],
})
export class MaterialModule {}
In my code, I have added Material module in imports section one time then same time i have added the same module in exports section.
So, i want to write code something like this:
importsAM = [
MatFormFieldModule,
MatSelectModule,
MatInputModule,
MatDatepickerModule,
]
@NgModule({
declarations: [],
imports: [
CommonModule,
importsAM
],
exports: [
importsAM
],
})
So, there is any way to write code something like that. or How can i reduce the duplicate code?
CodePudding user response:
Sure. Use the spread operator:
...
const modules = [
MatToolbarModule,
MatIconModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatSelectModule,
MatInputModule,
MatDatepickerModule
];
@NgModule({
declarations: [],
imports: [
CommonModule,
...modules
],
exports: [
...modules
],
})
export class MaterialModule {}
If exports
does not have any other modules, you could also go with exports: modules
.