2017-02-27 73 views
2

我解決了這個問題,因爲它沒有解決類似問題中的問題。沒有MdDialogRef的供應商

我仍然有錯誤'沒有供應商MdDialogRef'即使我跟着官方教程一步一步。

我有兩個組件。第一部分:

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, 
    { 
    } 
    ); 
    dialogRef.afterClosed().subscribe(result => { 
     this.selectedOption = result; 
    }); 
    } 

我的第二個組成部分(對話):

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

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

export class DocumentDialogComponent { 
    constructor(
     public dialogRef: MdDialogRef<DocumentDialogComponent> 
    ) {} 
} 

而我的模塊配置:

import { MaterialModule } from "@angular/material"; 
    import { DocumentsListComponent } from './documents-list.component'; 
    import { DocumentDialogComponent } from './document-dialog.component'; 

    imports : [ 
    MaterialModule.forRoot() 
    ], 

    declarations: [ 
     AppComponent, 
     DocumentListComponent, 
     DocumentDialogComponent 
    ], 
    entryComponents: [ 
     AppComponent, 
     DocumentListComponent, 
     DocumentDialogComponent 
    ], 
    providers: [ 
    ], 
    bootstrap: [ 
     AppComponent 
    ] 

爲什麼還是有錯誤:

Error in ./DocumentsListComponent class DocumentsListComponent - inline template:0:167 caused by: No provider for MdDialogRef! 
+1

你在哪裏進口'MaterialModule'? –

+0

在導入部分。我更新了我的問題 – Kivo

+0

您是否嘗試導入'MaterialModule'而不是'MaterialModule.forRoot()'? – gsc

回答

4

我刪除了我的模板中的<DocumentsListComponent></DocumentsListComponent>標記,現在它可以工作。

2

我們可以通過使用componentInstance財產DocumentDialogComponent設置參考消除此錯誤。

與其將MdDialogRef注入到組件中,我們可以通過從open方法返回的引用的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() {} 
}