angular material没有MatDialog的提供者!错误

svi*_*g12 6 modal-dialog angular-material angular

我正在尝试使用Angular材质模块在单击按钮时打开模型.我按照https://material.angular.io/components/dialog/examples中的建议操作了示例.

但是,我看到尝试运行应用程序时,我看到以下错误 -

errors.js:48 ERROR Error: Uncaught (in promise): Error: StaticInjectorError[MatDialog]: 
  StaticInjectorError[MatDialog]: 
    NullInjectorError: No provider for MatDialog!
Error: StaticInjectorError[MatDialog]: 
  StaticInjectorError[MatDialog]: 
    NullInjectorError: No provider for MatDialog!
    at _NullInjector.get (injector.js:31)
    at resolveToken (injector.js:387)
    at tryResolveToken (injector.js:330)
    at StaticInjector.get (injector.js:170)
    at resolveToken (injector.js:387)
    at tryResolveToken (injector.js:330)
    at StaticInjector.get (injector.js:170)
    at resolveNgModuleDep (ng_module.js:103)
    at NgModuleRef_.get (refs.js:1037)
    at resolveDep (provider.js:455)
    at _NullInjector.get (injector.js:31)
    at resolveToken (injector.js:387)
    at tryResolveToken (injector.js:330)
    at StaticInjector.get (injector.js:170)
    at resolveToken (injector.js:387)
    at tryResolveToken (injector.js:330)
    at StaticInjector.get (injector.js:170)
    at resolveNgModuleDep (ng_module.js:103)
    at NgModuleRef_.get (refs.js:1037)
    at resolveDep (provider.js:455)
    at resolvePromise (zone.js:824)
    at resolvePromise (zone.js:795)
    at eval (zone.js:873)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (ng_zone.js:575)
    at ZoneDelegate.invokeTask (zone.js:424)
    at Zone.runTask (zone.js:192)
    at drainMicroTaskQueue (zone.js:602)
    at ZoneTask.invokeTask [as invoke] (zone.js:503)
    at invokeTask (zone.js:1540)
defaultErrorLogger @ errors.js:48
Run Code Online (Sandbox Code Playgroud)

我有以下项目文件的设置

app.module.ts

我已导入MatDialogModule并在导入中也放置了相同的内容.在Entry组件中,我添加了需要在模态中呈现的组件.

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

@NgModule({
  imports:      [ BrowserModule, BrowserAnimationsModule, FormsModule, NgbModule.forRoot(), ReactiveFormsModule, 
                  HttpClientModule, routing, MatTabsModule, MatDialogModule ],
  declarations: [ AppComponent, AppHeaderComponent, SignInComponent, RecruitmentHomeComponent, JobTemplatesComponent, CreateJobTemplateComponent ],
  bootstrap:    [ AppComponent ],
  entryComponents: [CreateJobTemplateComponent],
  providers: [AuthGuard, UserService, AuthenticationService]
})
Run Code Online (Sandbox Code Playgroud)

job-templates.component.ts中的代码

这是一个文件,负责在单击添加到其模板的按钮时调用模型.

import { Component, OnInit } from '@angular/core';
import { CreateJobTemplateComponent } from './create-job-template/create-job-template.component';
import { MatDialog } from '@angular/material';


@Component({
  selector: 'app-job-templates',
  templateUrl: './job-templates.component.html',
  styleUrls: ['./job-templates.component.css']
})
export class JobTemplatesComponent implements OnInit {

  constructor(public dialog: MatDialog) { }

  ngOnInit() {
  }

  createjobtemplate(){
    let dialogRef = this.dialog.open(CreateJobTemplateComponent, {
      width: '250px'
    });
    //Set the dialogRef property of opened dialog with the obtained ref
    dialogRef.componentInstance.dialogRef = dialogRef;
  }

}
Run Code Online (Sandbox Code Playgroud)

创建在职template.component.ts

该组件将在模态对话框中呈现自身.

import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material';
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-create-job-template',
  templateUrl: './create-job-template.component.html',
  styleUrls: ['./create-job-template.component.css']
})
export class CreateJobTemplateComponent implements OnInit {

  form: FormGroup;

  constructor(private formBuilder: FormBuilder, public dialogRef: MatDialogRef<CreateJobTemplateComponent>) { }

  ngOnInit() {
    this.form = this.formBuilder.group({
      filename: ''
    })
  }

  submit(form) {
    this.dialogRef.close(`${form.value.filename}`);
  }

}
Run Code Online (Sandbox Code Playgroud)

任何人都可以让我知道我在哪里错了吗?我查找的大多数错误都是关于Inject MatDialogRef的失败,但我发现MatDialog有这个错误.

此外,我也在浏览这个博客,以确保我遵循相同的步骤. https://blog.thoughtram.io/angular/2017/11/13/easy-dialogs-with-angular-material.html

作为参考,我有代码

https://stackblitz.com/edit/angular-fhhmff

ted*_*ear 5

有同样的错误。原来我正在导入

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

本来应该

import { MatDialog } from '@angular/material/dialog';


sga*_*abb 2

看来您忘记在构造函数中添加 MAT_DIALOG_DATA 的注入器:

@Inject(MAT_DIALOG_DATA) 公共数据:任意

CreateJobTemplateComponent中,您需要导入 MAT_DIALOG_DATA:

import { MAT_DIALOG_DATA } from '@angular/material';
Run Code Online (Sandbox Code Playgroud)

构造函数应该是:

    constructor(@Inject(MAT_DIALOG_DATA) public data: any,
                private formBuilder: FormBuilder,
                public dialogRef: MatDialogRef<CreateJobTemplateComponent>) { }
Run Code Online (Sandbox Code Playgroud)

  • 您也应该防止将“null”数据传递给对话框。请参阅这篇文章:https://github.com/angular/material2/issues/4086#issuecomment-294476532 (3认同)