无法解析MatDialogRef angular 4的所有参数

Yaz*_*rez 9 angular-material angular

我正在研究Angular 4,我正在尝试设置材料包,在这里我试图尝试对话,但它不起作用可能是因为材料包我不确定.

这是我的(dialog.components.ts):

import {Component, OnInit} from '@angular/core';
import {MatDialogRef} from '@angular/material'

@Component({
    selector: 'app-dialog',
    templateUrl: './dialog.component.html',
    styleUrls: ['./dialog.component.css']
})
export class DialogComponent implements OnInit {

    public receivedNode: any;

    constructor(public dialogRef: MatDialogRef<DialogComponent>) {
    }

    ngOnInit() {
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的模块中:

import {MatButtonModule,MatMenuModule,MatToolbarModule,MatIconModule,MatCardModule, MatDialogRef} from '@angular/material';


 @NgModule({
        imports: [
            CommonModule,
            MatButtonModule,
            MatMenuModule,
            MatToolbarModule,
            MatIconModule,
            MatCardModule,
            RouterModule.forRoot(
                appRoutes,
                {enableTracing: true}
            ),
        ],
        declarations: [],
        exports: [
            MatButtonModule,
            MatMenuModule,
            MatToolbarModule,
            MatIconModule,
            MatCardModule
        ],
        entryComponents: [DialogComponent],
        providers: [MatDialogRef]
    })
    export class DialogModule {
    }
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误: 未捕获错误:无法解析MatDialogRef的所有参数:(?,?,?).

有任何想法吗?

编辑

我的通话功能:

openPopup(){
        const config = new MatDialogConfig();
        const dialogRef: MatDialogRef<DialogComponent> = this.dialog.open(DialogComponent, config);

        dialogRef.componentInstance.receivedNode = "test";
        console.log("test");
    }
Run Code Online (Sandbox Code Playgroud)

小智 16

MatDialogRef从提供商列表中删除.

这不是提供者,这是对Object的引用(以它结尾Ref)


小智 8

您只需要在提供程序中添加它。对于我来说,下面的代码有效。如果您使用了 MAT_DIALOG_DATA 那么您也需要添加它。

providers: [
{ 
provide: MatDialogRef,
useValue: []
 }, 
{ 
provide: MAT_DIALOG_DATA, 
useValue: [] 
}
]
Run Code Online (Sandbox Code Playgroud)


Swo*_*oox 6

希望这有助于我拥有的东西:

组件:

import { MatDialog } from '@angular/material';
import { DialogComponent } from './dialogs'; // component name of dialog can add multiple in here.

@Component({
    selector: 'yourComponent',
    templateUrl: './yourComponent.html'
})
export class YourComponent {

  private dialogRef: any;
  constructor(public dialog: MatDialog) {

  openPopup(){
    this.dialogRef = this.dialog.open(DialogComponent , {
                                    width: '250px',
                                    height: '25%',
                                    data: { errorcode: errorCode, errormessage: errorMessage }
                                });
                                this.dialogRef.updatePosition({ top: '3%', left: '20%' });
  }
Run Code Online (Sandbox Code Playgroud)

在模块中:

import { DialogComponent } from './dialogs'; // component name of dialog
import { NgModule, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MatDialogModule } from '@angular/material';

@NgModule({
    declarations: [
        DialogComponent 
    ],
    imports: [
        BrowserModule,
        MatDialogModule
    ],
    entryComponents: [
        DialogComponent 
    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
Run Code Online (Sandbox Code Playgroud)

最后一个对话框:

import {Component, OnInit} from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

@Component({
    selector: 'app-dialog',
    templateUrl: './dialog.component.html',
    styleUrls: ['./dialog.component.css']
})
export class DialogComponent implements OnInit {

    constructor(public dialogRef: MatDialogRef<DialogComponent>, 
                @Inject(MAT_DIALOG_DATA) private data: any) { } // this.data.errormessage contain error message. Not sure if need OnInit.

    ngOnInit() {
    } // I not use this I put the data in html and click on buttons there to interact with the component.

}
Run Code Online (Sandbox Code Playgroud)