【问题标题】:Uncaught (in promise): Error: Export of name 'ngForm' not found未捕获(承诺):错误:未找到名称“ngForm”的导出
【发布时间】:2023-03-12 05:11:01
【问题描述】:

我正在尝试使用 ngModel 进行数据绑定,但是,我得到一个错误 ngForm not found。我已经在 app.module.ts 中包含了 formsmodule,如下所示。当我删除 #grantAccessForm="ngForm" 时错误消失,但是当我这样做并在输入字段中输入数据并提交时,我的页面会刷新并且刷新的页面在 url 中有参数,如下所示:“ http://localhost:8100/signup?name=a&userName=a&email=a@email.com&password=aa&confirm=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 {}

注册页面.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>

注册页面.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);
    
  }

}

【问题讨论】:

    标签: angular forms ionic-framework ngmodel


    【解决方案1】:

    硬重置应用程序网址解决了我的错误

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    我遇到了同样的问题。我通过将使用 ngForm 的组件添加到 app.module.ts 的声明列表中来解决它。

    【讨论】:

      【解决方案3】:

      此问题的有效解决方案:

      我遇到了同样的问题,要解决此问题,您可以将formsModule 导入到app.module.ts 或直接在组件的.module.ts 文件中。

      没有人说的最重要的事情是您必须导入您在app.module.ts 文件中创建的组件,如果您不这样做,它将无法工作并且您的ngForm 将不会被检测到

      p>

      所以请确保您将组件模块导入到app.module.ts。这样我就可以解决问题了。并且花了我几个小时进行调查,没有人说。

      【讨论】:

        【解决方案4】:

        在下面的代码中,这个问题与“role”属性有关:

        <!-- Wrong -->
        <form role="form" (ngSubmit)="save(userForm.value)" userForm="ngForm"></form>   
        
        <!-- Correct -->
        <form (ngSubmit)="save(userForm.value)" #userForm="ngForm"></form>
        

        【讨论】:

        • 你能提供更多关于你的代码做什么的信息吗?还提供诸如两个代码块之间的差异之类的信息将产生更好的质量答案。
        【解决方案5】:

        对我来说,终止服务器并重新启动它解决了这个问题。

        有时 Angular 的行为也像 Microsoft 一样,这很有趣——什么都不做,只需重新启动,一切就开始工作了……

        【讨论】:

          【解决方案6】:

          我在组件中使用反应式表单时遇到了这个问题。我在单元测试用例执行中发现了这个错误。

          简单的解决方案是在imports 部分添加ReactiveFormsModule

          beforeEach(async () => {
            await TestBed.configureTestingModule({
              declarations: [ YourComponent ],
              imports: [AppRoutingModule,ReactiveFormsModule],
              providers: [ AuthService],
            })
            .compileComponents();
          });
          

          这将解决您的问题。

          【讨论】:

          • 在我的情况下,我需要 FormsModule,但您的回答提示我方向正确
          【解决方案7】:

          我在下面的步骤中遇到了同样的问题,它对我有用

          • 从两个地方为应用模块和工作当前模块导入 FormsModule'
          • 在你的 ts 文件中导入 ngForm 然后执行以下步骤
          1. 终止控制台
          2. 运行 npm 安装
          3. 再次运行服务器

          【讨论】:

            【解决方案8】:

            我通过以下第一种方式解决了同样的问题。

            我从两个不同的模块中调用了一个通用组件(弹出组件),例如一个是患者模块,另一个是医生模块, 我在患者模块中导入了常见的“弹出组件”,但没有在医生模块中导入。

            所以当我从医生模块调用常见的“弹出组件”时,这就是问题,它会产生错误,因为它不是在医生模块中导入的

            ERROR 错误:找不到名称“ngForm”的导出

            所以首先你需要检查你是否已经从你调用它的那个模块中导入了组件。 …………

            第二种方式

            我只是在主 app.module.ts 中导入通用的“弹出组件”,然后从患者和医生模块中删除。

            它也对我有用 我认为第二种方式最好。

            试试这个希望你的问题能解决

            【讨论】:

              【解决方案9】:

              以防万一有人遇到类似问题...即使FormsModule 正确导入到您自己的模块中,也会收到错误Error: Export of name 'ngForm' not found

              如果您的表单声明位于 a; 上,也会发生这种情况。说; div 而不是 form 元素。

              喜欢

                  <form #myForm="ngForm"></form>
              

                  <div #myForm="ngForm"></div>
              

              【讨论】:

                【解决方案10】:

                我在作为延迟加载模块的一部分的组件中遇到了类似的问题。尽管我将 FormsModule 和 ReactiveFormsModule 导入到我的模块中并为该组件添加了路由,但我忘记在声明中添加该组件。在那里添加组件修复它

                【讨论】:

                  【解决方案11】:

                  好吧,我遇到了同样的错误并设法修复它。因此,首先,如果您分离了模块或创建了一个额外的模块或在app.modulets.ts 文件中,您需要将您的组件添加到声明中并导入导入。 经验: 在createdExtra.module.tsapp.modulets.ts

                  @NgModule({
                    declarations: [...
                                    componetWithNgForm,
                                   ...
                                   ],
                    imports: [
                      ...
                      FormsModule
                    ]
                  

                  希望我能帮上忙。

                  【讨论】:

                    【解决方案12】:

                    FormsModule 被导入到你的 app.module.ts 文件中,就可以了。

                    放置“ngForm”指令后:

                    https://angular.io/api/forms/NgForm#listening-for-form-submission

                    根据链接中的示例模板,您必须将带有模板引用变量 (#var) 的 "ngModel" 指令放入所有表单元素中。

                    我努力为我工作。

                      <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" #inputName="ngModel"></ion-input>
                                </ion-item>
                    
                                <ion-item>
                                  <ion-input  name="userName" type="text" placeholder="Username"  [(ngModel)]="dataModel.username" #userName="ngModel"></ion-input>
                                </ion-item>
                    
                                <ion-item>
                                  <ion-input name="email" type="email" placeholder="your@email.com"  [(ngModel)]="dataModel.email" #inputEmail="ngModel"></ion-input>
                                </ion-item>
                    
                                <ion-item>
                                  <ion-input name="password" type="password" placeholder="Password"  [(ngModel)]="dataModel.password" #inputPassword="ngModel"></ion-input>
                                </ion-item>
                    
                                <ion-item>
                                  <ion-input name="confirm" type="password" placeholder="Password again"  [(ngModel)]="dataModel.passwordAgain" #inputPassword2="ngModel"></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>
                    

                    【讨论】:

                      【解决方案13】:

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

                      【讨论】:

                      • 更多信息在这里:NG0301
                      【解决方案14】:

                      我终止了服务器。执行 npm install 命令并再次尝试。问题已解决。

                      【讨论】:

                        【解决方案15】:

                        经过反复试验,我的问题是保存我的表单组件的模块被延迟加载。

                        【讨论】:

                          【解决方案16】:

                          我遇到了类似的问题,在我的情况下,我试图从 ModalController 打开该组件。

                          解决方案在 app.module.ts 下的 imports 下,您需要添加 FormsModule 并且您需要在声明数组中添加组件。

                          下面提到的 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 {SignUpPageComponent} from './sign-up-page.component';
                          import { AppRoutingModule } from './app-routing.module';
                          import { FormsModule } from '@angular/forms';
                          import { ReactiveFormsModule } from '@angular/forms';
                          
                          @NgModule({
                            declarations: [AppComponent,SignUpPageComponent],
                            entryComponents: [],
                            imports: [
                              BrowserModule,
                              FormsModule,
                              IonicModule.forRoot(), 
                              AppRoutingModule, 
                              ReactiveFormsModule
                            ],
                            exports:[],
                          
                            providers: [
                              StatusBar,
                              SplashScreen,
                              { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
                            ],
                            bootstrap: [AppComponent]
                          })
                          export class AppModule {}
                          

                          【讨论】:

                            【解决方案17】:

                            在 app.module.ts 中添加 FormsModule:

                            import { BrowserModule } from '@angular/platform-browser';
                            import { NgModule } from '@angular/core';
                            import { FormsModule } from '@angular/forms';
                            import { HttpClientModule } from '@angular/common/http';
                            import { AppComponent } from './app.component';
                            
                            @NgModule({
                              declarations: [
                                AppComponent
                              ],
                              imports: [
                                BrowserModule,
                                FormsModule,
                                HttpClientModule
                              ],
                              providers: [],
                              bootstrap: [AppComponent]
                            })
                            export class AppModule { }
                            

                            【讨论】:

                              【解决方案18】:
                              1. 在这里您错过了 dataModel.name 的拼写 ngmodel。
                              2. 更改答案中给出的 ion-button html。

                              sign-up-page.component.html

                              <ion-content padding>
                                <h2>Welcome to Ionic!</h2>
                                <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>
                                            **<!-- Here you miss spelled ngModel -->**
                                            <ion-input  name="name" type="text" placeholder="Name" [(ngModel)]="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> 
                                          **<!-- Here change button html -->**
                                          <button ion-button  size="large" type="submit" expand="block" >Register</button>
                                        </div>
                                      </ion-col>
                                    </ion-row>
                                  </ion-grid>
                                </form>
                              </ion-content>
                              

                              【讨论】:

                              猜你喜欢
                              • 2022-06-19
                              • 1970-01-01
                              • 2017-10-04
                              • 2023-02-06
                              • 2017-10-05
                              • 1970-01-01
                              • 2019-02-18
                              • 2022-12-19
                              • 2018-03-12
                              相关资源
                              最近更新 更多