Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 没有MdDialogRef的提供程序_Angular_Mddialog - Fatal编程技术网

Angular 没有MdDialogRef的提供程序

Angular 没有MdDialogRef的提供程序,angular,mddialog,Angular,Mddialog,我重新回答这个问题,因为在类似的讨论中它并没有解决我的问题 即使在我一步一步地遵循官方教程之后,我仍然有错误“没有MdDialogRef的提供者” 我有两个部分。第一部分: 从“@angular/material”导入{MdDialog}; 从“./document dialog.component”导入{DocumentDialogComponent}; @组成部分({ 选择器:“文档列表”, 模板 }) 导出类DocumentsListComponent{ 建造师( 公共对话框:MdDial

我重新回答这个问题,因为在类似的讨论中它并没有解决我的问题

即使在我一步一步地遵循官方教程之后,我仍然有错误“没有MdDialogRef的提供者”

我有两个部分。第一部分:

从“@angular/material”导入{MdDialog};
从“./document dialog.component”导入{DocumentDialogComponent};
@组成部分({
选择器:“文档列表”,
模板
})
导出类DocumentsListComponent{
建造师(
公共对话框:MdDialog){
}
openFormDialog(){
让dialogRef=this.dialog.open(DocumentDialogComponent,
{
}
);
dialogRef.afterClosed().subscribe(结果=>{
this.selectedOption=结果;
});
}
我的第二个组件(对话框):

从“@angular/material”导入{MdDialogRef}”;
@组成部分({
选择器:“文档对话框”,
模板
})
导出类DocumentDialogComponent{
建造师(
公共dialogRef:MdDialogRef
) {}
}
和我的模块配置:

从“@angular/material”导入{MaterialModule};
从“./documents list.component”导入{DocumentsListComponent};
从“./document dialog.component”导入{DocumentDialogComponent};
进口:[
MaterialModule.forRoot()的
],
声明:[
应用组件,
文档列表组件,
文档对话框组件
],
入口组件:[
应用组件,
文档列表组件,
文档对话框组件
],
供应商:[
],
引导:[
应用组件
]
为什么我仍然有错误:

./DocumentsListComponent类DocumentsListComponent-内联模板中的
错误:0:167原因:没有MdDialogRef的提供程序!

我删除了模板中的
标记,现在它可以工作了。

我们可以通过使用componentInstance属性在DocumentDialogComponent中设置引用来消除此错误

我们不需要将MdDialogRef注入到组件中,而是可以通过打开方法返回的引用的componentInstance来设置其属性

以下是修改后的工作代码:

import { MdDialog} from "@angular/material";

import { DocumentDialogComponent } from './document-dialog.component';    

@Component({
  selector: 'documents-list',
  template
})
export class DocumentsListComponent {

  constructor(
     public dialog: MdDialog) {
  }

  openFormDialog() {

   let dialogRef  = this.dialog.open(DocumentDialogComponent);

    //set the reference here
    dialogRef.componentInstance.dRef = dialogRef;

   dialogRef.afterClosed().subscribe(result => {
      this.selectedOption = result;
   });
  }
对话框组件如下所示:

import { MdDialogRef} from "@angular/material";

@Component({
 selector: 'document-dialog',
 template
 })

export class DocumentDialogComponent {
    public dRef:MdDialogRef<DocumentDialogComponent>
    constructor() {}
}
从“@angular/material”导入{MdDialogRef}”;
@组成部分({
选择器:“文档对话框”,
模板
})
导出类DocumentDialogComponent{
公共dRef:MdDialogRef
构造函数(){}
}

您在哪里导入
材料模块
?在导入部分。我更新了我的问题。您是否尝试导入
材料模块
而不是
材料模块.forRoot()
?这在这里起作用您是否在
文档列表组件
中导入
文档对话框组件