can not find module “@angular/material”

前端 未结 7 703
余生分开走
余生分开走 2021-01-31 02:47

I\'m using Angular 2. When I\'m trying to import \"@angular/material\" i\'m getting error for this. I\'m importing packages like:

import {MdDialog} from \"@angul         


        
相关标签:
7条回答
  • Change to,

    import {MaterialModule} from '@angular/material';

    DEMO

    0 讨论(0)
  • 2021-01-31 03:06
    import {MatButtonModule} from '@angular/material/button';
    
    0 讨论(0)
  • 2021-01-31 03:08

    I followed each of the suggestions here (I'm using Angular 7), but nothing worked. My app refused to acknowledge that @angular/material existed, so it showed an error on this line:

    import { MatCheckboxModule } from '@angular/material';
    

    Even though I was using the --save parameter to add Angular Material to my project:

    npm install --save @angular/material @angular/cdk
    

    ...it refused to add anything to my "package.json" file.

    I even tried deleting the package-lock.json file, as some articles suggest that this causes problems, but this had no effect.

    To fix this issue, I had to manually add these two lines to my "package.json" file.

    {
        "devDependencies": {
            ...
            "@angular/material": "~7.2.2",
            "@angular/cdk": "~7.2.2",
            ...
    

    What I can't tell is whether this is an issue related to using Angular 7, or if it's been around for years....

    0 讨论(0)
  • Found this post: "Breaking changes" in angular 9. All modules must be imported separately. Also a fine module available there, thanks to @jeff-gilliland: https://stackoverflow.com/a/60111086/824622

    0 讨论(0)
  • 2021-01-31 03:14

    Follow these steps to begin using Angular Material.

    Step 1: Install Angular Material

    npm install --save @angular/material
    

    Step 2: Animations

    Some Material components depend on the Angular animations module in order to be able to do more advanced transitions. If you want these animations to work in your app, you have to install the @angular/animations module and include the BrowserAnimationsModule in your app.

    npm install --save @angular/animations
    

    Then

    import {BrowserAnimationsModule} from '@angular/platform browser/animations';
    
    @NgModule({
    ...
      imports: [BrowserAnimationsModule],
    ...
    })
    export class PizzaPartyAppModule { }
    

    Step 3: Import the component modules

    Import the NgModule for each component you want to use:

    import {MdButtonModule, MdCheckboxModule} from '@angular/material';
    
    @NgModule({
    ...
    imports: [MdButtonModule, MdCheckboxModule],
    ...
     })
     export class PizzaPartyAppModule { }
    

    be sure to import the Angular Material modules after Angular's BrowserModule, as the import order matters for NgModules

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { HttpModule } from '@angular/http';
    import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
    
    import {MdCardModule} from '@angular/material';
    @NgModule({
        declarations: [
            AppComponent,
            HeaderComponent,
            HomeComponent
        ],
        imports: [
            BrowserModule,
            FormsModule,
            HttpModule,
            MdCardModule
        ],
        providers: [],
        bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    Step 4: Include a theme

    Including a theme is required to apply all of the core and theme styles to your application.

    To get started with a prebuilt theme, include the following in your app's index.html:

    <link href="../node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">
    
    0 讨论(0)
  • 2021-01-31 03:16

    Please check Angular Getting started :)

    1. Install Angular Material and Angular CDK
    2. Animations - if you need
    3. Import the component modules

    and enjoy the {{Angular}}

    0 讨论(0)
提交回复
热议问题