How to create a custom Angular Material module? (M

2019-02-14 03:30发布

I used to import the Material library into the base module, app.module.ts, but Angular Material v2.0.0-beta.3 deprecates the Material module. According to the changelog (https://github.com/angular/material2/blob/master/CHANGELOG.md) you should now create a custom module that imports the individual Material components. I cannot make this work.

This approach:

@NgModule({
    declarations: [ MdInputModule ],
    imports: [
        CommonModule,
        MdInputModule
    ],
    exports: [ MdInputModule ]
})

export class FooMaterialModule {}

Causes this error:

Uncaught Error: Unexpected module 'MdInputModule' declared by the module 'FooMaterialModule'. Please add a @Pipe/@Directive/@Component annotation.

How do I make a custom module for the Angular Material library?

2条回答
爷的心禁止访问
2楼-- · 2019-02-14 04:00

You can create a feature module that looks like this:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MdInputModule } from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    MdInputModule
    BrowserAnimationsModule
  ]
})
export class CustomMaterialModule { }

Then import it in your app.module.ts:

import {CustomMaterialModule} from "./custom-material/custom-material.module";
...
@NgModule({
  ...
  imports: [
    ...
    CustomMaterialModule
  ]
  ...
})
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-14 04:06

Your custom Angular Material module can mirror the deprecated Material Module.

As the change log indicates, you would want to comment out Material components not used by your application.

We've found that, with the current state of tree-shaking in the world, that using an aggregate NgModule like MaterialModule leads to tools not being able to eliminate code for components that aren't used.

In order to ensure that users end up with the smallest code size possible, we're deprecating MaterialModule, to be removed in the a subsequent release.

To replace MaterialModule, users can create their own "Material" module within their application (e.g., GmailMaterialModule) that imports only the set of components actually used in the application.

my-material.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { MatAutocompleteModule } from '@angular/material'; 
import { MatButtonToggleModule } from '@angular/material';
import { MatButtonModule } from '@angular/material';
import { MatCheckboxModule } from '@angular/material';
import { MatDialogModule } from '@angular/material';  
import { MatListModule } from '@angular/material';  
import { MatGridListModule } from '@angular/material';
import { MatCardModule } from '@angular/material';  
import { MatChipsModule } from '@angular/material';  
import { MatIconModule } from '@angular/material';  
import { MatInputModule } from '@angular/material'; 
import { MatMenuModule } from '@angular/material';  
import { MatProgressSpinnerModule } from '@angular/material';
import { MatProgressBarModule } from '@angular/material'; 
import { MatRadioModule } from '@angular/material'; 
import { MatRippleModule } from '@angular/material';
import { MatSelectModule } from '@angular/material';  
import { MatSlideToggleModule } from '@angular/material';
import { MatSliderModule } from '@angular/material';  
import { MatSidenavModule } from '@angular/material'; 
import { MatSnackBarModule } from '@angular/material';
import { MatTabsModule } from '@angular/material';  
import { MatToolbarModule } from '@angular/material';  
import { MatTooltipModule } from '@angular/material';


const MATERIAL_MODULES = [
  CommonModule,
  MatAutocompleteModule,
  MatButtonModule,
  MatCardModule,
  MatChipsModule,
  MatCheckboxModule,
  MatGridListModule,
  MatInputModule,
  MatListModule,
  MatProgressBarModule,
  MatProgressSpinnerModule,
  MatRippleModule,
  MatSelectModule,
  MatSidenavModule,
  MatTabsModule,
  MatToolbarModule,

  // These modules include providers.
  MatButtonToggleModule,
  MatDialogModule,
  MatIconModule,
  MatMenuModule,
  MatRadioModule,
  MatSliderModule,
  MatSlideToggleModule,
  MatSnackBarModule,
  MatTooltipModule
];


@NgModule({
  imports: MATERIAL_MODULES,
  exports: MATERIAL_MODULES
})
export class MyMaterialModule { }

app.module.ts

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';

...

import { MyMaterialModule } from './my-material.module';


@NgModule({
  declarations: [ AppComponent ],
  imports: [
    BrowserAnimationsModule,
    BrowserModule,

    ...

    MyMaterialModule
    ],
  providers: [],
  bootstrap: [ AppComponent ]

})

export class AppModule { }
查看更多
登录 后发表回答