Can't resolve all parameters for MatDialogRef

2020-08-11 11:17发布

问题:

I'm working on Angular 4 and I'm trying to setup material package, and here I'm trying to try dialog, but it doesn't work maybe because of material package I'm not sure.

This is my (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() {
    }

}

And in my module:

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 {
    }

I got this error:

Any ideas?

EDIT

My call function:

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

        dialogRef.componentInstance.receivedNode = "test";
        console.log("test");
    }

回答1:

Hope this helps this is what I have done:

At the component:

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%' });
  }

In the module:

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]
})

And at last, the dialog(s):

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 have not used this. Instead, I have put the data in HTML and click on buttons from there to interact with the component.

}


回答2:

Remove MatDialogRef from the list of providers.

This isn't a provider, this is a reference to an Object (it ends with Ref)



回答3:

You need to add it in the providers only.For me below code worked .If you have used MAT_DIALOG_DATA then you need to add this as well.

providers: [
{ 
provide: MatDialogRef,
useValue: []
 }, 
{ 
provide: MAT_DIALOG_DATA, 
useValue: [] 
}
]