错误类型:无法读取未定义的属性(Reading' losplyEname')

发布于 2025-02-10 08:10:17 字数 1678 浏览 1 评论 0原文

Add-employee.html

<div class="col-md-6 offset-md-3">
    <h3> Create Employee </h3>
    <form (ngSubmit) = "onSubmit()">

        <div class="form-group">
            <label>Name</label>
            <input type="text" class ="form-control" id = "employeename"
                [(ngModel)] = "employee.employeename" name = "employeename">
        </div>

        <div class="form-group">
            <label>Age</label>
            <input  class ="form-control" id = "age"
                [(ngModel)] = "employee.age" name = "age">
        </div>

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

    </form>
</div> 

Add-employee.ts

import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { EmployeeserviceService } from '../employeeservice.service';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-add-employee',
  templateUrl: './add-employee.component.html',
  styleUrls: ['./add-employee.component.css']
})
export class AddEmployeeComponent implements OnInit {

  employee !: Employee;

  constructor(private employeeService:EmployeeserviceService) { }

  ngOnInit(): void {
  }

  onSubmit(){
    console.log(this.employee);

    this.employeeService.saveEmployee(this.employee).subscribe(data=>{
      console.log(data);
    })
  }

}

我是Angular的新手,我正在尝试为动手体验创建示例员工CRUD应用程序。但是,在创建API创建时,我会得到:

无法阅读Property ophoreeName

我不知道我在哪里做错了,任何人都可以帮助我解决这个问题吗?

提前致谢。

add-employee.html

<div class="col-md-6 offset-md-3">
    <h3> Create Employee </h3>
    <form (ngSubmit) = "onSubmit()">

        <div class="form-group">
            <label>Name</label>
            <input type="text" class ="form-control" id = "employeename"
                [(ngModel)] = "employee.employeename" name = "employeename">
        </div>

        <div class="form-group">
            <label>Age</label>
            <input  class ="form-control" id = "age"
                [(ngModel)] = "employee.age" name = "age">
        </div>

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

    </form>
</div> 

add-employee.ts

import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { EmployeeserviceService } from '../employeeservice.service';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-add-employee',
  templateUrl: './add-employee.component.html',
  styleUrls: ['./add-employee.component.css']
})
export class AddEmployeeComponent implements OnInit {

  employee !: Employee;

  constructor(private employeeService:EmployeeserviceService) { }

  ngOnInit(): void {
  }

  onSubmit(){
    console.log(this.employee);

    this.employeeService.saveEmployee(this.employee).subscribe(data=>{
      console.log(data);
    })
  }

}

I am new to Angular and I am trying to create a sample employee CRUD application for hands-on experience. But while creating POST API creation I am getting:

cannot read property employeename

I don't know where I done a mistake can anyone please help me solve this issue?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2025-02-17 08:10:17

评论中提到的第一个问题,您错过了,在使用它之前,将雇员实例初始化为employee变量。将实例初始化为员工变量将解决问题。

export class AddEmployeeComponent implements OnInit {

  ...

  ngOnInit(): void {
    this.employee = new Employee(0, "", 0);
  }

  ...

}

对于隐藏初始化年龄值(0),您可以考虑在雇员类中应用age属性,为无效。

export class Employee {
    id: number;
    employeename: string;
    age: number | null;

    constructor(id: number, employeename: string, age: number | null){
        this.id=id;
        this.employeename=employeename;
        this.age=age;
    }
}

并初始化agenull

export class AddEmployeeComponent implements OnInit {

 ...

  ngOnInit(): void {
    this.employee = new Employee(0, "", null);
  }

  ...
}

The first problem as mentioned in the comment that you miss out to initialize the Employee instance to employee variable before using it. Initialize the instance to employee variable would fix the issue.

export class AddEmployeeComponent implements OnInit {

  ...

  ngOnInit(): void {
    this.employee = new Employee(0, "", 0);
  }

  ...

}

For hiding the initialize age value (0), you may consider applying age property in Employee class as nullable.

export class Employee {
    id: number;
    employeename: string;
    age: number | null;

    constructor(id: number, employeename: string, age: number | null){
        this.id=id;
        this.employeename=employeename;
        this.age=age;
    }
}

And initialize age as null.

export class AddEmployeeComponent implements OnInit {

 ...

  ngOnInit(): void {
    this.employee = new Employee(0, "", null);
  }

  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文