模块'AppModule'导入的意外值'MatDialogRef' . 请添加@NgModule注释

我正在尝试在Angular中集成Angular Material但是会出现以下错误 . 该程序已成功编译,但在浏览器中遇到此问题 .

Uncaught Error: Unexpected value 'MatDialogRef' imported by the module 'AppModule'. Please add a @NgModule annotation.
    at syntaxError (compiler.js:485)
    at eval (compiler.js:15226)
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15201)
    at JitCompiler._loadModules (compiler.js:34385)
    at JitCompiler._compileModuleAndComponents (compiler.js:34346)
    at JitCompiler.compileModuleAsync (compiler.js:34240)
    at CompilerImpl.compileModuleAsync (platform-browser-dynamic.js:239)
    at PlatformRef.bootstrapModule (core.js:5551)
    at eval (main.ts:11)

我在app.module.ts中导入了MatDialogRef

import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';

imports: [
    BrowserModule,
    FormsModule,
    BrowserAnimationsModule,
    MatButtonModule,
    MatDialogModule,
    MatDialogRef,
    ReactiveFormsModule,
    HttpModule,
    HttpClientModule,
    AppRoutingModule
  ],

我还在保存对话框html的组件中导入了它 .

import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

导出类ComposeMailComponent实现OnInit {

constructor(private dialogRef: MatDialogRef<ComposeMailComponent>,
      @Inject(MAT_DIALOG_DATA) private data) {}

  ngOnInit() {
  }
}

回答(2)

2 years ago

你不需要在app.module.ts里面导入 MatDialogRef ,改变

import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';

import from '@angular/material';

并从 imports 数组中删除 MatDialogRef

2 years ago

在app.module.ts文件中添加这些

从'@ angular / material'导入;

导入:[MatDialogModule],

它会工作正常