垫子对话框不会作为弹出窗口打开

问题描述

我正在尝试使用 Mat-Dialog 打开一个弹出窗口,但它出现在页面的左侧。

我已按照官方材料网站 https://material.angular.io/components/dialog/overview

中的步骤操作

TS

import { MatDialog } from '@angular/material/dialog';
import { DialogComponent } from '../shared/dialog/dialog.component';

@Component({
  selector: 'header',templateUrl: './header.component.html',styleUrls: ['./header.component.css']
})
export class HeaderComponent {
  constructor(public dialog: MatDialog) {}

  signin(): void {
    this.dialog.open(DialogComponent);
  }
}

对话 TS

import { Component,OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'app-dialog',templateUrl: './dialog.component.html',styleUrls: ['./dialog.component.css']
})
export class DialogComponent implements OnInit {
  constructor(public dialogRef: MatDialogRef<DialogComponent>) {}

  ngOnInit(): void {
    console.log('dialog');
  }
}

应用模块

import { NgModule } from '@angular/core';
import { browserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { BodyComponent } from './body/body.component';
import { DialogComponent } from './shared/dialog/dialog.component';
import { browserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDialogModule } from '@angular/material/dialog';
import { MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';

@NgModule({
  declarations: [
    AppComponent,HeaderComponent,FooterComponent,BodyComponent,DialogComponent
  ],exports: [MatDialogModule],imports: [browserModule,AppRoutingModule,browserAnimationsModule],entryComponents: [DialogComponent],providers: [
    { provide: MAT_DIALOG_DEFAULT_OPTIONS,useValue: { hasBackdrop: true } }
  ],bootstrap: [AppComponent]
})
export class AppModule {}

我做错了什么吗?

解决方法

在您的 AppModule 中,为什么要尝试导出 MatDialogModule?将 MatDialogModule 包含在您的 imports 中并尝试。