我想在单击按钮时弹出对话框。我在entryComponents数组中添加了组件名称,但仍然出现此错误:
src / app / homepage / homepage.component.ts中的ERROR:29:19-错误TS2304:找不到名称“ ProductFormComponent”。 29 this.dialog.open(ProductFormComponent,dialogConfig);
app.modules.ts:
import {MatDialogModule} from '@angular/material/dialog';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
HomepageComponent,
ProductFormComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
MatCardModule,
ReactiveFormsModule,
MatInputModule,
MatFormFieldModule,
BrowserAnimationsModule,
MatSelectModule,
MatDatepickerModule,
MatNativeDateModule,
MatButtonModule,
MatIconModule,
MatDialogModule,
],
entryComponents: [ProductFormComponent],
providers: [ServiceService],
bootstrap: [AppComponent],
})
export class AppModule { }
Homepage.components.ts文件:
import { Component, OnInit } from '@angular/core';
import { MatDialog,MatDialogConfig,MatDialogRef} from '@angular/material/dialog';
@Component({
selector: 'app-homepage',
templateUrl: './homepage.component.html',
styleUrls: ['./homepage.component.css']
})
export class HomepageComponent implements OnInit {
constructor( private dialog: MatDialog ) { }
ngOnInit(): void {
}
oncreate(){
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
this.dialog.open(ProductFormComponent,dialogConfig);
}
}
Homepage.component.html:
<div class="button-row" >
<button mat-raised-button (click)="oncreate()">
<mat-icon>add</mat-icon>Create
</button>
</div>
product-form.component.ts:
import { Component, OnInit } from '@angular/core';
import {ServiceService} from '../../shared/service.service';
@Component({
selector: 'app-product-form',
templateUrl: './product-form.component.html',
styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
constructor(public service: ServiceService) { }
ngOnInit(): void {
}
}
答案 0 :(得分:0)
您需要将ProductFormComponent
导入Homepage.components.ts