Angular 16 - 无法绑定到“formGroup”,因为它不是“form”的已知属性

问题描述 投票:0回答:0

我已经有一个正在运行的 Angular 应用程序,因为有功能扩展我必须为应用程序实现路由和登录页面。以前没有路由,作为路由的一部分,我添加了以下代码。

app.modules.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { SignUpComponent } from './sign-up/sign-up.component';
import { LoginComponent } from './login/login.component';
import {CommonModule} from "@angular/common";
import {FormsModule , ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    SignUpComponent,
    LoginComponent,

  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    CommonModule,
    ReactiveFormsModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

注册.componnent.ts

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';

@Component({
  selector: 'app-sign-up',
  templateUrl: './sign-up.component.html',
  styleUrls: ['./sign-up.component.css']
})


export class SignUpComponent implements  OnInit{

  public signupFrom : FormGroup|any;
  constructor(private formBuilder: FormBuilder) {}


  items : Array<any> = [
    {name:"username", type:"text", data:"Username"},
    {name:"password", type:"password",data:"Password"},
    {name:"email", type:"text",data:"Email"}

  ]



  ngOnInit(): void {
    this.signupFrom = new FormGroup({

      'Username' : new FormControl(),
      'Passoword' : new FormControl(),
      'Email' : new FormControl()
    })
  }
}

注册.component.htm

<div class="row">
  <div class="mx-auto col-10 col-md-8 col-lg-6">
    <!-- Form -->
    <form [formGroup]="signupFrom" class="form-example"  >
      <div *ngFor="let item of items " class="form-group"  >
        <label for="{{item.name}}">{{item.data}}</label>
        <input

          type="{{item.type}}"
          class="form-control {{item.name}}"
          id="{{item.name}}"
          placeholder="{{item.data}}..."
          name="{{item.data}}"
        />
      </div>



      <button type="submit" class="btn btn-primary btn-customized mt-4">
        Sign Up
      </button>
    </form>
    <!-- Form end -->
  </div>
</div>

当我尝试使用 ng-serve 执行时,出现以下错误: 无法绑定到“formGroup”,因为它不是“form”的已知属性。

html css angular typescript algorithm
© www.soinside.com 2019 - 2024. All rights reserved.