【问题标题】:FormComponent.html:8 ERROR TypeError: Cannot read property 'userName' of undefinedFormComponent.html:8 错误类型错误:无法读取未定义的属性“用户名”
【发布时间】:2019-02-17 20:00:27
【问题描述】:

我正在使用 Angular 7 并遵循 form.component.html 代码:

{{ userForm.value | json}}
{{ 用户模型 | json}}
    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="userName" [(ngModel)]="userModel.userName">
    </div>

    <div class="form-group">
            <label>Email</label>
            <input type="text" class="form-control" name="email" [(ngModel)]="userModel.email">
    </div>


    <div class="form-group">
            <label>Mobile number</label>
            <input type="number" class="form-control" name="number" [(ngModel)]="userModel.number">
    </div>


    <div class="form-group">
                <label>Address</label>
                <input type="text" class="form-control" name="address" [(ngModel)]="userModel.address">
    </div>

    <button class="btn btn-primary" type="submit">Submit Order</button>

user.js 代码

export class User {
    constructor(
        public userName: string,
        public email: string,
        public number: string,
        public address: string
    ){}
}

app.component.ts:

import { Component } from '@angular/core';
import { User } from './User';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title=['Angular','Vue','React'];

  userModel = new User('ash','ash@gmail.com','01233','asasas');
}

当我编译时,我得到错误“用户名”未定义。

【问题讨论】:

  • 你收到userForm is undefineduserName undefined
  • 我刚刚复制了您的代码并在 stackblitz 中运行。很好。见stackblitz.com/edit/…
  • @dileepkumarjami 用户名未定义
  • 是的,你的代码是对的。我认为导入用户是错的
  • 因为您的代码在 stackblitz 中完美运行。我也遇到过这个问题。我只是重新运行程序。我不确定它是否适用于非但重新运行的项目。

标签: javascript jquery html angular


【解决方案1】:
 <input formControlName="userName" placeholder="Name" class="form-control">

在每个输入字段中使用它,它会起作用。

【讨论】:

    【解决方案2】:

    像下面这样改变你的用户类

    export class User {
            public userName: string;
            public email: string;
            public number: string;
            public address: string;
        constructor(userName, email, number, address){
            this.userName = userName;
            this.email = email;
            this.number = number;
            this.address = address;
         }
    }
    

    在这里查看更多关于typescript classes.的信息

    正如@Hameed Syed 所说,再次对您的类导入语句进行交叉检查。

    更新

    您的代码对我来说似乎很好,无论如何尝试使用 Angular 的生命周期方法进行数据绑定。

    import { Component, OnInit } from '@angular/core';
    import { User } from './User';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
      title=['Angular','Vue','React'];
    
      userModel: User;
    
      ngOnInit(){
        this.userModel = new User('ash','ash@gmail.com','01233','asasas');
      }
    }
    

    【讨论】:

    • 解释一些事情并投票会让我清楚你的想法..
    • 我改变了,但错误出现在控制台中。' FormComponent.html:8 ERROR TypeError: Cannot read property 'userName' of undefined 我不知道为什么
    猜你喜欢
    • 2021-02-21
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2018-03-31
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多