出现错误,因为即使我在entryComponents数组中添加了组件,也找不到名称“ ProductFormComponent”

时间:2020-10-02 07:25:03

标签: angular typescript

我想在单击按钮时弹出对话框。我在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 {
   }
}

1 个答案:

答案 0 :(得分:0)

您需要将ProductFormComponent导入Homepage.components.ts