简体   繁体   English

为什么无法使用 Angular Material CDK 正确显示对话框?

[英]Why can't show dialogs correctly using Angular Material CDK?

I'm trying to show a dialog in an Angular 15 project.我正在尝试在 Angular 15 项目中显示一个对话框。 I would like to use the Angular Material Component Development Kit package (I don't use Material components), even following the official documentation and building the examples shown I always have the same problem: the component that should being shown in a modal dialog is added and shown on the page instead.我想使用Angular Material Component Development Kit package(我不使用 Material 组件),即使遵循官方文档并构建显示的示例我总是遇到同样的问题:应该在模态对话框中显示的组件是添加并显示在页面上。 What am I doing wrong?我究竟做错了什么?

app.module.ts:应用程序模块.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DialogModule } from '@angular/cdk/dialog';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, DialogModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

app.component.ts:应用程序组件.ts:

import { Dialog, DIALOG_DATA } from '@angular/cdk/dialog';
import { Component, Inject } from '@angular/core';

export interface DialogData {
  animal: 'panda' | 'unicorn' | 'lion';
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  constructor(public dialog: Dialog) {}

  openDialog() {
    this.dialog.open(CdkDialogDataExampleDialog, {
      minWidth: '300px',
      data: {
        animal: 'panda',
      },
    });
  }
}

@Component({
  selector: 'cdk-dialog-overview-example-dialog',
  templateUrl: './cdk-dialog-overview-example-dialog.html',
  styleUrls: ['./cdk-dialog-overview-example-dialog.css'],
})
export class CdkDialogDataExampleDialog {
  constructor(@Inject(DIALOG_DATA) public data: DialogData) {}
}

app.component.html:应用程序组件.html:

<button (click)="openDialog()">Open dialog</button>

cdk-dialog-overview-example-dialog.html: cdk-dialog-overview-example-dialog.html:

<h1>Favorite Animal</h1>
<div>
  My favorite animal is:
  <ul>
    <li><span *ngIf="data.animal === 'panda'">&#10003;</span> Panda</li>
    <li><span *ngIf="data.animal === 'unicorn'">&#10003;</span> Unicorn</li>
    <li><span *ngIf="data.animal === 'lion'">&#10003;</span> Lion</li>
  </ul>
</div>

cdk-dialog-overview-example-dialog.css cdk-dialog-overview-example-dialog.css

:host {
  display: block;
  background: #fff;
  border-radius: 8px;
  padding: 8px 16px;
}

As said from @Eliseo in his comment, I forgot to include @import '@angular/cdk/overlay-prebuilt.css' in my style.css .正如@Eliseo 在他的评论中所说,我忘记在我的style.css中包含@import '@angular/cdk/overlay-prebuilt.css'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular + Material 不能在对话框中使用指令 - Angular + Material can't use derectives in dialogs Angular 材质 cdk 拖放:无法绑定到 'cdkDragFreeDragPosition' - Angular material cdk drag drop: Can't bind to 'cdkDragFreeDragPosition' 如何解决 angular 材料错误:无法解析“@angular/cdk/bidi”? - how to resolve angular material Error: Can't resolve '@angular/cdk/bidi'? 无法正确插值数据-Angular材质表-Angular2 + - Can't interpolate data correctly - Angular material table - Angular2+ 使用角度示例无法正确显示角度材质树 - Angular Material Tree isn't displayed correctly using angular example 角度拖放cdk在角度材质对话框中不起作用 - angular drag and drop cdk doesn't work in angular material dialog 我在 Angular 9 中安装了 Material Design,为什么我无法显示元素? - I Installed Material Design in Angular 9, why can't I get the elements to show? 通过@angular/cdk-experimental 在 Angular Material 2 表中使用虚拟滚动 - Using Virtual Scroll in Angular Material 2 Table with @angular/cdk-experimental 材料 CDK Overlay flexibleConnectedTo 不接受 Angular 组件作为原点 - Material CDK Overlay flexibleConnectedTo doesn't accept Angular component as origin 无法使用离子和角材料滚动 - Can't scroll using Ionic and Angular Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM