Angular 9或10 + Angular Material 10:无法将导入解析为NgModule类

时间:2020-07-26 11:15:01

标签: angular angular-material angular-ivy

我使用Angular 9和最新版本的材料进行了一个新项目。当我导入一些Material的类时,出现此错误:

node_modules/@angular/material/form-field/label.d.ts:10:22中的错误-错误NG6002:出现在MaterialModule的NgModule.imports中,但无法解析为NgModule类。

这可能意味着声明cc的MatLabel库(@ angular / material / form-field)未被ngcc正确处理,或者与Angular Ivy不兼容。检查是否有较新版本的库,如果有,则进行更新。还可以考虑与该库的作者进行检查,以查看该库是否与Ivy兼容。

10个导出声明类MatLabel { ~~~~~~~~~ node_modules/@angular/material/form-field/hint.d.ts:20:22-错误NG6002:出现在MaterialModule的NgModule.imports中,但无法解析为NgModule类。

这可能意味着ngcc尚未正确处理声明MatHint的库(@ angular / material / form-field),或与Angular Ivy不兼容。检查是否有较新版本的库,如果有,则进行更新。还可以考虑与该库的作者进行检查,以查看该库是否与Ivy兼容。

20 export声明类MatHint { ~~~~~~~ src / app / material.module.ts:12:14-错误NG6002:出现在AppModule的NgModule.imports中,但无法解析为NgModule类。

是否缺少@NgModule注释?

12个导出类MaterialModule {} ~~~~~~~~~~~~~~

node_modules/@angular/material/form-field/label.d.ts:10:22中的错误-错误NG6002:出现在MaterialModule的NgModule.imports中,但无法解析为NgModule类。

这可能意味着声明cc的MatLabel库(@ angular / material / form-field)未被ngcc正确处理,或者与Angular Ivy不兼容。检查是否有较新版本的库,如果有,则进行更新。还可以考虑与该库的作者进行检查,以查看该库是否与Ivy兼容。

10个导出声明类MatLabel { ~~~~~~~~~ node_modules/@angular/material/form-field/hint.d.ts:20:22-错误NG6002:出现在MaterialModule的NgModule.imports中,但无法解析为NgModule类。

这可能意味着ngcc尚未正确处理声明MatHint的库(@ angular / material / form-field),或与Angular Ivy不兼容。检查是否有较新版本的库,如果有,则进行更新。还可以考虑与该库的作者进行检查,以查看该库是否与Ivy兼容。

20 export声明类MatHint { ~~~~~~~ src / app / material.module.ts:12:14-错误NG6002:出现在AppModule的NgModule.imports中,但无法解析为NgModule类。

是否缺少@NgModule注释?

12个导出类MaterialModule {} ~~~~~~~~~~~~~~

material.module.ts

import { NgModule } from '@angular/core';
import {
  MatFormFieldModule,
  MatLabel,
  MatHint,
} from '@angular/material/form-field';

@NgModule({
  imports: [MatFormFieldModule, MatLabel, MatHint],
  exports: [MatFormFieldModule, MatLabel, MatHint],
})
export class MaterialModule {}

app.modules.ts

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

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

import { MaterialModule } from './material.module';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CarouselComponent } from './carousel/carousel.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { CrudComponent } from './crud/crud.component';
import { ProfileComponent } from './profile/profile.component';
import { LayoutComponent } from './layout/layout.component';
import { FooterComponent } from './layout/footer/footer.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { LoginComponent } from './login/login.component';
import { PrivHeaderComponent } from './layout/priv-header/priv-header.component';
import { PrivMenuComponent } from './layout/priv-menu/priv-menu.component';
import { MainMenuComponent } from './layout/main-menu/main-menu.component';
import { MainHeaderComponent } from './layout/main-header/main-header.component';

@NgModule({
  declarations: [
    AppComponent,
    CarouselComponent,
    DashboardComponent,
    CrudComponent,
    ProfileComponent,
    LayoutComponent,
    FooterComponent,
    HomeComponent,
    AboutComponent,
    LoginComponent,
    PrivHeaderComponent,
    PrivMenuComponent,
    MainMenuComponent,
    MainHeaderComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    MaterialModule,
    ReactiveFormsModule,
    AppRoutingModule,
    BrowserAnimationsModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class AppModule {}

package.json

{
  "name": "mini-aplicacion",
  "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/animations": "~10.0.5",
    "@angular/cdk": "^10.1.0",
    "@angular/common": "~10.0.5",
    "@angular/compiler": "~10.0.5",
    "@angular/core": "~10.0.5",
    "@angular/forms": "~10.0.5",
    "@angular/material": "^10.1.0",
    "@angular/platform-browser": "~10.0.5",
    "@angular/platform-browser-dynamic": "~10.0.5",
    "@angular/router": "~10.0.5",
    "rxjs": "~6.5.4",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.4",
    "@angular/cli": "~10.0.4",
    "@angular/compiler-cli": "~10.0.5",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.7"
  }
}

我试图删除node_modules并重新安装。另外,我将Angular版本更新为10,然后将Material版本更新为Material版本,但是问题仍然存在。

我查看了其他类似问题,但没有一种解决方案可以解决我的问题。

1 个答案:

答案 0 :(得分:1)

MatLabel和MatHint包含在MatFormFieldModule中,您不必导入它们。按照in the documentation

的说明仅导入MatFormFieldModule
import {MatFormFieldModule} from '@angular/material/form-field'; 

@NgModule({
  imports: [MatFormFieldModule],
  exports: [MatFormFieldModule],
})