MatDialogRef 没有提供者 [英] No provider for MatDialogRef
问题描述
我使用的是材料 2 版本 2.0.0-beta.12.我需要调整材质Modal的位置.我按照这个答案来做到这一点.https://stackoverflow.com/a/44238699/8253445 由于 MdDialog 现在不可用,我使用了 {MatDialog, MatDialogRef,MAT_DIALOG_DATA}.
constructor(public dialog: MatDialog,public dialModRef:MatDialogRef) {}
当我将 MatDialogRef 添加到我的构造函数时,它给了我No provider for MatDialogRef"错误.请问你能帮我解决这个问题吗?
dialog-overview-example.ts
import {Component, Inject} from '@angular/core';导入 {MatDialog, MatDialogRef, MAT_DIALOG_DATA}'@角度/材料';@成分({选择器:'对话框概述-示例',templateUrl: 'dialog-overview-example.html'})导出类 DialogOverviewExample {动物:绳子;名称:字符串;构造函数(公共对话框:MatDialog,公共dialModRef:MatDialogRef) {}openDialog(): void {让 dialogRef = this.dialog.open(DialogOverviewExampleDialog, {宽度:'500px',数据:{名称:this.name,动物:this.animal}});dialogRef.afterClosed().subscribe(result => {console.log('对话框已关闭');this.animal = 结果;});}}@成分({选择器:'对话框概述-示例-对话框',templateUrl: 'dialog-overview-example-dialog.html',})导出类 DialogOverviewExampleDialog {构造函数(public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) 公共数据:任意) { }onNoClick(): void {this.dialogRef.close();}}
app.module.ts
entryComponents: [DialogOverviewExample,DialogOverviewExampleDialog]
dialog-overview-example-dialog.html
嗨{{data.name}}
<div md-dialog-content><p>你最喜欢的动物是什么?</p><md-form-field><input mdInput tabindex="1" [(ngModel)]="data.animal"></md-form-field>
<div md-dialog-actions><button md-button tabindex="2">确定</button><button md-button (click)="onNoClick()" tabindex="-1">不用了</button>
你会得到准确的
<块引用>MatDialogRef
如果您将对话框的组件 html 选择器包含在 dialog-overview-example.html
中时出错,例如 <dialog-overview-example-dialog></dialog-overview-example-对话框>
没有必要,不要在任何地方包含对话框组件的 html 选择器.
I am using material 2 version 2.0.0-beta.12. I need to adjust the position of the material Modal. I followed this answer to do that. https://stackoverflow.com/a/44238699/8253445 Since MdDialog is not available now I used {MatDialog, MatDialogRef, MAT_DIALOG_DATA}.
constructor(public dialog: MatDialog,public dialModRef:MatDialogRef<any>) {}
When I add MatDialogRef to my constructor it gives me " No provider for MatDialogRef" error. Please can you help me to figure this out?
dialog-overview-example.ts
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from
'@angular/material';
@Component({
selector: 'dialog-overview-example',
templateUrl: 'dialog-overview-example.html'
})
export class DialogOverviewExample {
animal: string;
name: string;
constructor(public dialog: MatDialog,public
dialModRef:MatDialogRef<any>) {}
openDialog(): void {
let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '500px',
data: { name: this.name, animal: this.animal }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
}
@Component({
selector: 'dialog-overview-example-dialog',
templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
onNoClick(): void {
this.dialogRef.close();
}
}
app.module.ts
entryComponents: [DialogOverviewExample,DialogOverviewExampleDialog]
dialog-overview-example-dialog.html
<h1 md-dialog-title>Hi {{data.name}}</h1>
<div md-dialog-content>
<p>What's your favorite animal?</p>
<md-form-field>
<input mdInput tabindex="1" [(ngModel)]="data.animal">
</md-form-field>
</div>
<div md-dialog-actions>
<button md-button tabindex="2">Ok</button>
<button md-button (click)="onNoClick()" tabindex="-1">No Thanks</button>
</div>
You will get that exact
No provider for
MatDialogRef
error in case you include your dialog's component html selector in dialog-overview-example.html
like <dialog-overview-example-dialog></dialog-overview-example-dialog>
There is no need for that, do not include the html selector of the dialog component anywhere.
这篇关于MatDialogRef 没有提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!