简体   繁体   English

无法导入“@angular/material”模块

[英]Not able to import "@angular/material" module

I'm totally new to mean stack and I'm facing some problems with Angular's material module.我对堆栈完全陌生,我正面临着 Angular 的材料模块的一些问题。 I'm trying to import the "@angular/material" module in my code but I'm getting an error whenever I'm importing it.我正在尝试在我的代码中导入“@angular/material”模块,但每次导入时都会出错。 The error is as follows:错误如下:

ERROR in src/app/app.module.ts:5:32 - error TS2306: File '/Users/anmolsarraf/Desktop/MEAN Stack/mean-course/node_modules/@angular/material/index
.d.ts' is not a module.

Here is my package.json file:这是我的 package.json 文件:

   {
  "name": "mean-course",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animation": "^4.0.0-beta.8",
    "@angular/animations": "~8.2.7",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "~8.2.7",
    "@angular/compiler": "~8.2.7",
    "@angular/core": "~8.2.7",
    "@angular/forms": "~8.2.7",
    "@angular/material": "^9.0.0",
    "@angular/platform-browser": "~8.2.7",
    "@angular/platform-browser-dynamic": "~8.2.7",
    "@angular/router": "~8.2.7",
    "rxjs": "~6.4.0",
    "tslib": "^1.10.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.5",
    "@angular/cli": "~8.3.5",
    "@angular/compiler-cli": "~8.2.7",
    "@angular/language-service": "~8.2.7",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.5.3"
  }
}

Here is my app.module.ts file:这是我的 app.module.ts 文件:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatInputModule } from '@angular/material/input';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PostCreateComponent } from './posts/post-create/post-create.component';

@NgModule({
  declarations: [
    AppComponent,
    PostCreateComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

To be exact I'm trying to do import { MatInputModule } from '@angular/material';确切地说,我正在尝试import { MatInputModule } from '@angular/material'; and I'm getting the above-mentioned error.我收到了上述错误。

I have tried importing the above module as import { MatInputModule } from '@angular/material/input';我尝试将上述模块import { MatInputModule } from '@angular/material/input';import { MatInputModule } from '@angular/material/input'; but then it throws a bunch of error saying node_modules/@angular/material/input/input.d.ts:138:9 - error TS1086: An accessor cannot be declared in an ambient context.但随后它抛出了一堆错误,说node_modules/@angular/material/input/input.d.ts:138:9 - error TS1086: An accessor cannot be declared in an ambient context.

Any help would be highly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

UPDATE更新

I created a new instance of an Angular app and then tried to import angular material in it, somehow it worked since I'm not getting any errors while importing it.我创建了一个 Angular 应用程序的新实例,然后尝试在其中导入 angular 材料,不知何故它起作用了,因为我在导入它时没有收到任何错误。 Thanks!谢谢!

Create one new module.ts file.创建一个新的 module.ts 文件。 for example, you can create material.module.ts and prepare all the material libraries in it like below例如,您可以创建 material.module.ts 并准备其中的所有材料库,如下所示

import { NgModule } from '@angular/core';
import { A11yModule } from '@angular/cdk/a11y';
import { ClipboardModule } from '@angular/cdk/clipboard';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { PortalModule } from '@angular/cdk/portal';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { CdkStepperModule } from '@angular/cdk/stepper';
import { CdkTableModule } from '@angular/cdk/table';
import { CdkTreeModule } from '@angular/cdk/tree';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatBadgeModule } from '@angular/material/badge';
import { MatBottomSheetModule } from '@angular/material/bottom-sheet';
import { MatButtonModule } from '@angular/material/button';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatCardModule } from '@angular/material/card';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatChipsModule } from '@angular/material/chips';
import { MatStepperModule } from '@angular/material/stepper';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatDialogModule } from '@angular/material/dialog';
import { MatDividerModule } from '@angular/material/divider';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatListModule } from '@angular/material/list';
import { MatMenuModule } from '@angular/material/menu';
import { MatNativeDateModule, MatRippleModule } from '@angular/material/core';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatRadioModule } from '@angular/material/radio';
import { MatSelectModule } from '@angular/material/select';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatSliderModule } from '@angular/material/slider';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { MatTabsModule } from '@angular/material/tabs';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatTreeModule } from '@angular/material/tree';

@NgModule({
  exports: [
    A11yModule,
    ClipboardModule,
    CdkStepperModule,
    CdkTableModule,
    CdkTreeModule,
    DragDropModule,
    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,
    PortalModule,
    ScrollingModule
  ],
  imports: [
    A11yModule,
    ClipboardModule,
    CdkStepperModule,
    CdkTableModule,
    CdkTreeModule,
    DragDropModule,
    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,
    PortalModule,
    ScrollingModule
  ]
})
export class MaterialModule { }

import this model in the app.module.ts在 app.module.ts 中导入这个模型

@NgModule({
  declarations: [
    AppComponent        
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,       
    MaterialModule    
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

You need to be more specific:你需要更加详细:

import {MatInputModule} from '@angular/material/input';

not just不只是

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

and then add it to your imports然后将其添加到您的导入中

Did you run the command ng add @angular/material?你运行命令ng add @angular/material? ? ?

You can also try going through the angular material docs here: https://material.angular.io/guide/getting-started您也可以尝试在此处浏览 angular material 文档: https : //material.angular.io/guide/getting-started

Also, are you taking the mean course by Maximilian Schwarzmüller?另外,您是否正在参加马克西米利安·施瓦茨米勒的平均课程? If you're not too far into it, try seeing if you missed any steps (I'm guessing because the name "mean-course" is used for his project too :)如果您不太深入,请尝试查看您是否遗漏了任何步骤(我猜是因为他的项目也使用了名称“mean-course”:)

EDIT: In your imports module, you put the line import { MatInputModule } from '@angular/material/input';编辑:在您的导入模块中,您将行import { MatInputModule } from '@angular/material/input'; but forgot to add it to your imports array :)但忘了将它添加到您的导入数组:)

change it to this:把它改成这样:


@NgModule({
  declarations: [
    AppComponent,
    PostCreateComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    BrowserAnimationsModule,
    MatInputModule //new import
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Edit 2: if that still doesn't work, try running this command: ng update --next @angular/cli --force or updating typescript using npm install -g typescript@latest or npm update编辑 2:如果仍然不起作用,请尝试运行此命令: ng update --next @angular/cli --force或使用npm install -g typescript@latestnpm update npm install -g typescript@latest

Seems there are so many version mismatch in your package.json.似乎您的 package.json 中有很多版本不匹配。

Can your try ng update , then you will see something like this:你可以试试ng update ,然后你会看到这样的:

Using package manager: 'npm'
Collecting installed dependencies...
Found 80 dependencies.
We analyzed your package.json, there are some packages to update:

  Name                                Version                  Command to update
 ---------------------------------------------------------------------------------
  @angular/cdk                        7.3.7 -> 10.1.3          ng update @angular/cdk
  @angular/cli                        8.3.25 -> 10.0.8         ng update @angular/cli
  @angular/core                       8.2.14 -> 10.0.14        ng update @angular/core
  @ngrx/store                         7.4.0 -> 10.0.0          ng update @ngrx/store
  rxjs                                6.5.4 -> 6.6.2           ng update rxjs

Then please update the required dependencies然后请更新所需的依赖项

首先确保您使用ng add @angular/material导入了材料,然后只需右键单击您的项目,从磁盘重新加载,错误就会消失..

I had the same issue as the OP.我和 OP 有同样的问题。 I could not pin point the cause but I believe it may have been an issue with the package manager adding conflicting versions of the Material package.我无法指出原因,但我相信这可能是包管理器添加了 Material 包的冲突版本的问题。 I recently switched to using PNPM over NPM.我最近改用 PNPM 而不是 NPM。 I first started to see this issue after running pnpm install @angular/material .在运行pnpm install @angular/material后,我第一次开始看到这个问题。 The package installed successfully but I had the errors the OP had above whenever I ran the server, ng serve .该软件包已成功安装,但是每当我运行服务器ng serve时,就会出现 OP 上面的错误。 Like the OP I tried all the suggestions but none worked.像 OP 一样,我尝试了所有建议,但都没有奏效。 I even tried installing the specific version of Material that worked previously pnpm i @angular/material@8.2.3 but that didn't work either.我什至尝试安装以前可以使用pnpm i @angular/material@8.2.3的特定版本的 Material,但这也不起作用。

What worked was globally setting PNPM as my package manager in the Anglar CLI configurations .有效的是在 Anglar CLI 配置中将 PNPM全局设置为我的包管理器

ng config -g cli.packageManager pnpm // Set PNPM globally
ng config cli.packageManager pnpm    // Set PNPM for single project
ng config -g cli.packageManager npm  // Set NPM globally, default

Afterwards, I created a new project and installed the Material package using ng add @angular/material .之后,我创建了一个新项目并使用ng add @angular/material安装了 Material 包。 It took a while but it everything installed successfully.花了一段时间,但一切都安装成功。 In both instances PNMP was used instead of NPM.在这两种情况下,都使用 PNMP 代替 NPM。 I was also able to import the Material modules using the old syntax.我还能够使用旧语法导入 Material 模块。

import {MatCardModule, MatIconModule, MatToolbarModule} from '@angular/material'

对我来说,它通过从“@angular/material/input”导入 MatInputModule 来解决。

import {MatInputModule} from '@angular/material/input';

I had also faced the same issue when I started using Angular Material for the first time and didn't know the installation process.当我第一次开始使用 Angular Material 并且不知道安装过程时,我也遇到了同样的问题。

First, you need to install angular material by command:首先需要通过命令安装angular material:

npm install --save @angular/material

Then add angular material in your project with this command:然后使用以下命令在您的项目中添加角度材料:

ng add @angular/material

For me, it started working by these two simple commands.对我来说,它开始通过这两个简单的命令工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM