我试图学习Angular并在我的简单应用程序中收到一个错误打开对话框。我已经在各处搜索了此错误的来源,但找不到原因。
应用模块
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { MatListModule } from "@angular/material/list";
import { MatToolbarModule } from "@angular/material/toolbar";
import { FlexLayoutModule } from "@angular/flex-layout";
import { MatGridListModule } from "@angular/material/grid-list";
import { MatCardModule } from "@angular/material/card";
import { MatButtonModule } from "@angular/material/button";
import "hammerjs";
import { MenuComponent } from "./menu/menu.component";
import { DishdetailComponent } from "./dishdetail/dishdetail.component";
import { DishService } from "./services/dish.service";
import { PromotionService } from "./services/promotion.service";
import { LeaderService } from "./services/leader.service";
import { HeaderComponent } from "./header/header.component";
import { FooterComponent } from "./footer/footer.component";
import { AboutComponent } from "./about/about.component";
import { HomeComponent } from "./home/home.component";
import { ContactComponent } from "./contact/contact.component";
import { AppRoutingModule } from "./app-routing/app-routing.module";
import { LoginComponent } from "./login/login.component";
import { MatDialogModule } from "@angular/material/dialog";
@NgModule({
declarations: [
AppComponent,
MenuComponent,
DishdetailComponent,
HeaderComponent,
FooterComponent,
AboutComponent,
HomeComponent,
ContactComponent,
LoginComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatToolbarModule,
MatListModule,
FlexLayoutModule,
MatGridListModule,
MatCardModule,
MatButtonModule,
AppRoutingModule,
MatDialogModule,
],
providers: [DishService, PromotionService, LeaderService],
bootstrap: [AppComponent],
entryComponents: [LoginComponent],
})
export class AppModule {}
HeaderComponent:包含一个按钮的组件,该按钮将打开登录表单对话框。
import { Component, OnInit } from "@angular/core";
import { MatDialog, MatDialogRef } from "@angular/material";
import { LoginComponent } from "../login/login.component";
@Component({
selector: "app-header",
templateUrl: "./header.component.html",
styleUrls: ["./header.component.scss"],
})
export class HeaderComponent implements OnInit {
constructor(public dialog: MatDialog) {}
ngOnInit(): void {}
openLoginForm() {
this.dialog.open(LoginComponent, { width: "500px", height: "450px" });
}
}
LoginComponent:代表登录表单的对话框组件
import { Component, OnInit } from "@angular/core";
import { MatDialog, MatDialogRef } from "@angular/material";
@Component({
selector: "app-login",
templateUrl: "./login.component.html",
styleUrls: ["./login.component.scss"],
})
export class LoginComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}