【问题标题】:StaticInjectionError for matDialogRefmatDialogRef 的 StaticInjectionError
【发布时间】:2019-10-20 18:49:32
【问题描述】:

我正在开发一个带有角度前端的项目,并试图创建一个 matDialogue,但是我总是收到错误:

StaticInjectorError(AppModule)[CustomersComponent -> MatDialogRef]: 
  StaticInjectorError(Platform: core)[CustomersComponent -> MatDialogRef]: 
    NullInjectorError: No provider for MatDialogRef!

我在 html 文件中放入的内容无关紧要,即使我将其留空,我也会收到错误消息。 这是我的 ts 文件:

import {Component, OnInit} from '@angular/core';
import {AuthService} from '../../services/auth.service';
import {MatDialog, MatDialogRef, MatDialogModule} from '@angular/material/dialog';
import {FormBuilder, FormGroup} from '@angular/forms';

@Component({
  selector: 'app-customers',
  styleUrls: ['./customers.component.scss'],
  templateUrl: './customers.component.html'
})
export class CustomersComponent implements OnInit {

  customerCreateForm: FormGroup;

  constructor(private authService: AuthService,
              private custDialog: MatDialogRef<CustomersComponent>,
              private fb: FormBuilder) {
  }

  ngOnInit() {
  }

任何想法可能导致这种情况?

【问题讨论】:

  • 什么是客户组件,打开对话框的组件,或对话框中的组件?
  • MatDialogRef 用于对打开的对话框执行操作,例如关闭它的广告。 StaticInjectionError 意味着客户组件没有打开的对话框。如果您共享打开对话框的代码或描述您的确切用例,则更容易发现问题。
  • 您的CustomersComponent所属的模块中是否导入了MatDialogRef?
  • 请提供 stackblitz 示例!

标签: angular typescript dialog angular-material frontend


【解决方案1】:

我不确定我是否正确,但正如我在错误中看到的那样,它写的是没有提供 MatDialogRef。

肯定是因为进口。请检查您是否在 app.module.ts 文件中导入了 MatDialogModule。

import {MatDialogModule} from '@angular/material/dialog';
@NgModule({ 
imports:[MatDialogModule] 
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多