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() {
}
}
在我的模块中:
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 {
}
我有一个错误: 任何想法? 我的呼叫功能:
openPopup(){
const config = new MatDialogConfig();
const dialogRef: MatDialogRef<DialogComponent> = this.dialog.open(DialogComponent, config);
dialogRef.componentInstance.receivedNode = "test";
console.log("test");
}
从提供商列表中删除。
这不是提供商,这是对一个对象的引用(以
MatDialogRef
)
您只需要在提供商中添加它。对于我下面的代码工作。如果您使用Mat_dialog_data,则还需要添加此。
Ref
希望这有助于这是我所做的:组件:
在模块中:
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%' });
}
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]
})
I在角9
时遇到了相同的误差(
可以解析matdialogRef的所有参数:(?,?)。),尤其是当测试使用角材料
模块执行测试时。
这是我的解决方案()()部分:
before:
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.
}
After
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
beforeEach(async(() => {
TestBed.configureTestingModule({
...
...
providers: [
{ provide: MatDialogRef },
{ provide: MAT_DIALOG_DATA }
]
}).compileComponents();
}));
在app.module.ts,添加useValue:{}
import { MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';
beforeEach(async(() => {
TestBed.configureTestingModule({
...
...
providers: [{
provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {hasBackdrop: false}
}]
}).compileComponents();