gpt4 book ai didi

angular - 未捕获( promise ): Error: Export of name 'ngForm' not found

转载 作者:行者123 更新时间:2023-12-03 14:35:56 25 4
gpt4 key购买 nike

我正在尝试使用 ngModel 进行数据绑定(bind),但是,我得到一个错误 ngForm not found。我已经在 app.module.ts 中包含了 formsmodule,如下所示。当我删除 时错误消失#grantAccessForm=“ngForm”但是当我这样做并在输入字段中输入数据并提交时,我的页面会刷新并且刷新的页面在 url 中有参数,如下所示:“http://localhost:8100/signup?name=a&userName=a&email=a@email .com&密码=aa&确认=aa"
我希望能够在 onSubmit() 函数中执行某些操作而不刷新页面,有人可以向我解释一下 #grantAccessForm=“ngForm”这意味着以及为什么我会收到此错误。

app-routing.module.ts

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { LoginPageComponent } from './login/login-page/login-page.component';
import { SignUpPageComponent } from './login/sign-up-page/sign-up-page.component';

const routes: Routes = [
{
path: 'home',
loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)
},
{
path: '',
component: LoginPageComponent
},
{
path: 'login',
component: LoginPageComponent,

//()=> import('./login/login-page/login-page.component').then(m=> m.LoginPageComponent)
},
{
path: 'signup',
component: SignUpPageComponent
}
];

@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts

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

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
FormsModule,
IonicModule.forRoot(),
AppRoutingModule,
ReactiveFormsModule
],
exports:[],

providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}

sign-up-page.component.html

</ion-content>
<form #grantAccessForm= "ngForm" (ngSubmit)="onSubmit(grantAccessForm)">
<ion-grid>
<ion-row justify-content-center>
<ion-col align-self-center size-md="6" size-lg="5" size-xs="12">
<div class="centerText">
<h3>Create your account!</h3>
</div>
<div padding>

<ion-item>
<ion-input name="name" type="text" placeholder="Name" [(ngMode)]="dataModel.name"></ion-input>
</ion-item>

<ion-item>
<ion-input name="userName" type="text" placeholder="Username" [(ngModel)]="dataModel.username"></ion-input>
</ion-item>

<ion-item>
<ion-input name="email" type="email" placeholder="your@email.com" [(ngModel)]="dataModel.email"></ion-input>
</ion-item>

<ion-item>
<ion-input name="password" type="password" placeholder="Password" [(ngModel)]="dataModel.password"></ion-input>
</ion-item>

<ion-item>
<ion-input name="confirm" type="password" placeholder="Password again" [(ngModel)]="dataModel.passwordAgain"></ion-input>
</ion-item>
</div>
<div padding>
<ion-button size="large" type="submit" expand="block" >Register</ion-button>
</div>
</ion-col>
</ion-row>
</ion-grid>
</form>
</ion-content>

sign-up-page.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@Component({
selector: 'app-sign-up-page',
templateUrl: './sign-up-page.component.html',
styleUrls: ['./sign-up-page.component.scss'],
})
export class SignUpPageComponent implements OnInit {

// Import ViewChild
@ViewChild('grantAccessForm', {static: false}) grantAccessForm: NgForm;
dataModel: any = {}; // Your data model


constructor() { }

ngOnInit() {}

onSubmit(form) {
console.log(form);

}

}

最佳答案

我有同样的问题,所以你需要在 app.module.ts 中添加你的 singUpComponent

关于angular - 未捕获( promise ): Error: Export of name 'ngForm' not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com