3

I start, learning the Angular with Google Material Design, so far so good, I create my front-end app with the official documentation from https://angular.io/tutorial and https://material.angular.io/guides websites.

I read a similar question but that won't work for me, my app uses the Angular 7.0.0 with Angular Material 7.0.0.

So, I want to use most of the Angular Material Components in my demo app, what is the best way to have, all of them.

I read many articles and tutorials like Getting Started With Angular Material 2 or this one, but in all of them, they just using the basic components or the tutorial is about old version of Angular Material and the current official documentation doesn't provide the list of available component to Import for current version and how to use those components by the way?

My app.module.ts with basic component:(Updated code)

// angular component and 
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from "@angular/forms";

// my components
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { NavigationComponent } from "./header/navigation/navigation.component";
import { ContainerComponent } from './container/container.component';
import { NavComponent } from './nav/nav.component';


// add animaitons for material 
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
// add CDK layout for material
import { LayoutModule } from "@angular/cdk/layout";
// add material to module
import { MaterialModule } from "./class/material.module"

@NgModule({
  imports: [
    BrowserModule,
    FormsModule, ReactiveFormsModule,
    BrowserAnimationsModule,
    LayoutModule,
    MaterialModule
  ],
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    NavigationComponent,
    ContainerComponent,
    NavComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

I find this code in similar question, but when is add it in a separate file and then importing to the app.module.ts, my app stop working and just I will see the loading message in the browser. (Updated code)

import { NgModule } from "@angular/core";

import { 
  MatButtonModule, 
  MatToolbarModule, 
  MatSidenavModule, 
  MatCheckboxModule,
  MatIconModule,
  MatListModule
 } from "@angular/material";
@NgModule({
  imports: [
    MatButtonModule,
    MatToolbarModule,
    MatSidenavModule,
    MatCheckboxModule,
    MatIconModule,
    MatListModule
  ],
  exports: [
    MatButtonModule,
    MatToolbarModule,
    MatSidenavModule,
    MatCheckboxModule,
    MatIconModule,
    MatListModule
  ]
})
export class MyMaterialModule {}

(Updated) For now, I just fix the material.module.ts in my class folder, but still, I can't add other components. like the following components:

import {
  MatNativeDateModule,
  MatSnackBarModule,
  MatDialogModule,
  MatTableModule,
  MatPaginatorModule,
  MatSortModule,
  MatTabsModule,
  MatCheckboxModule,
  MatCard,
  MatCardModule,
  MatFormField,
  MatFormFieldModule,
  MatProgressSpinnerModule,
  MatInputModule
} from "@angular/material";
import { MatDatepickerModule } from "@angular/material/datepicker";
import { MatRadioModule } from "@angular/material/radio";
import { MatSelectModule } from "@angular/material/select";
import { MatSliderModule } from "@angular/material/slider";
import { MatDividerModule } from "@angular/material/divider";
Nazari
  • 438
  • 1
  • 9
  • 20
  • your 'MaterialModule ' file looks fine here. You just need to import this module in AppModule (or any other feature module) in order to use it. Obviously you don't need to import all those detail material modules in AppModule any more. – Joseph Wu Oct 23 '18 at 05:32
  • I import it by using this line of code `import { MaterialModule } from "./class/material"`, but app stop working and displaying the first Material component as an error in the console. – Nazari Oct 23 '18 at 05:37
  • can you share your updated AppModule code? – Joseph Wu Oct 23 '18 at 05:53
  • Joe Wu, please check my code, I just update it. – Nazari Oct 23 '18 at 06:21
  • Can you change the module name from 'MaterialModule' to something different and try? say, 'MyMaterialModule' instead. I wonder that will help. – Joseph Wu Oct 23 '18 at 06:30
  • Nice idea, but I try that, with another name or location. Everything is okay with basic components. So when I add another component to the app, the app will stop working, why I don't know! – Nazari Oct 23 '18 at 06:35
  • can you open Chrome console dev window and take a screenshot of the error message? (or copy and paste, maybe the front portion of the errors) – Joseph Wu Oct 23 '18 at 06:39
  • Yes, I will do that. – Nazari Oct 23 '18 at 06:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182322/discussion-between-joe-wu-and-alireza-nazari). – Joseph Wu Oct 23 '18 at 06:45

1 Answers1

1

You need to use comman module while using seprate file:

1] material.module.ts

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

import { CdkTableModule } from '@angular/cdk/table';
import { CdkTreeModule } from '@angular/cdk/tree';

import {
  MatAutocompleteModule,
  MatBadgeModule,
  MatBottomSheetModule,
  MatButtonModule,
  MatButtonToggleModule,
  MatCardModule,
  MatCheckboxModule,
  MatChipsModule,
  MatDatepickerModule,
  MatDialogModule,
  MatDividerModule,
  MatExpansionModule,
  MatGridListModule,
  MatIconModule,
  MatInputModule,
  MatListModule,
  MatMenuModule,
  MatNativeDateModule,
  MatPaginatorModule,
  MatProgressBarModule,
  MatProgressSpinnerModule,
  MatRadioModule,
  MatRippleModule,
  MatSelectModule,
  MatSidenavModule,
  MatSliderModule,
  MatSlideToggleModule,
  MatSnackBarModule,
  MatSortModule,
  MatStepperModule,
  MatTableModule,
  MatTabsModule,
  MatToolbarModule,
  MatTooltipModule,
  MatTreeModule
} from '@angular/material';

@NgModule({
  exports: [
    CdkTableModule,
    CdkTreeModule,
    MatAutocompleteModule,
    MatBadgeModule,
    MatBottomSheetModule,
    MatButtonModule,
    MatButtonToggleModule,
    MatCardModule,
    MatCheckboxModule,
    MatChipsModule,
    MatStepperModule,
    MatDatepickerModule,
    MatDialogModule,
    MatDividerModule,
    MatExpansionModule,
    MatGridListModule,
    MatIconModule,
    MatInputModule,
    MatListModule,
    MatMenuModule,
    MatNativeDateModule,
    MatPaginatorModule,
    MatProgressBarModule,
    MatProgressSpinnerModule,
    MatRadioModule,
    MatRippleModule,
    MatSelectModule,
    MatSidenavModule,
    MatSliderModule,
    MatSlideToggleModule,
    MatSnackBarModule,
    MatSortModule,
    MatTableModule,
    MatTabsModule,
    MatToolbarModule,
    MatTooltipModule,
    MatTreeModule
  ],
  imports: [CommonModule],
  declarations: []
})
export class MaterialModule {}
  1. app.module.ts

import { BrowserModule } from '@angular/platform-browser';

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

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

import { NgForm,ReactiveFormsModule } from '@angular/forms';

import {
  HttpClient,
  HTTP_INTERCEPTORS,
  HttpClientModule
} from '@angular/common/http';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from './material/material.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { NavigationComponent } from "./header/navigation/navigation.component";
import { ContainerComponent } from './container/container.component';
import { NavComponent } from './nav/nav.component';
import { LayoutModule } from "@angular/cdk/layout";
@NgModule({
  declarations: [
   AppComponent,
    HeaderComponent,
    FooterComponent,
    NavigationComponent,
    ContainerComponent,
    NavComponent,
    NgForm
  ],
  imports: [
    CommonModule,
    MaterialModule,
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    ReactiveFormsModule,
    HttpClientModule,
LayoutModule
  ],
  providers: [ ],
  bootstrap: [AppComponent]
})
export class AppModule { }
Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39
  • I add it by method 1 and now my app is loading. the console displays the error message `Uncaught Error: Unexpected directive 'MatCard' imported by the module 'MaterialModule'. Please add a @NgModule annotation.` – Nazari Oct 23 '18 at 05:33
  • I have added updated code for material.module.ts please try again – Dinesh Ghule Oct 23 '18 at 05:37
  • same error `Uncaught Error: Unexpected directive 'MatCard' imported by the module 'MaterialModule'. Please add a @NgModule annotation. at syntaxError (compiler.js:1021) at compiler.js:10604 at Array.forEach () at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (compiler.js:10579) at ` – Nazari Oct 23 '18 at 05:42
  • I update files as you update and then remove the one duplicated line and import the NgModule also remove the NgForm, but app stop building this time. I reset all file and move some of them to a new file then everything work again. – Nazari Oct 23 '18 at 06:10
  • Dinesh Ghule, Please check my code I updated them, still, I cant add other components, your solution didn't work. I just rest the files to the first place. – Nazari Oct 23 '18 at 06:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182324/discussion-between-dinesh-ghule-and-alireza-nazari). – Dinesh Ghule Oct 23 '18 at 06:58