[英]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'">✓</span> Panda</li>
<li><span *ngIf="data.animal === 'unicorn'">✓</span> Unicorn</li>
<li><span *ngIf="data.animal === 'lion'">✓</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.