【问题标题】:Property 'emp' does not exist on type 'employee[ ] - Angular 6 ng serve --prod类型'employee []上不存在属性'emp' - Angular 6 ng serve --prod
【发布时间】:2018-12-19 04:48:22
【问题描述】:

在 ng serve --prod 显示错误时运行命令 ng serve 工作正常

ERROR in src\app\employee\employee.component.html(12,9): : Property 'emp' does not exist on type 'employee[]'.
src\app\employee\employee.component.html(23,9): : Property 'emp' does not exist on type 'employee[]'.
src\app\employee\employee.component.html(36,9): : Property 'emp' does not exist on type 'employee[]'.

Employee.component.html

    <h2 mat-dialog-title>Edit details</h2>
<div mat-dialog-content>
  <form
    fxLayout="column"
    [formGroup]="editForm"
    #f="ngForm">
  <div class="input-row">
      <mat-form-field fxFlex>
        <mat-label>Id</mat-label>
        <input readonly 
        value="{{data.emp.id}}"
          matInput #id disabled
          formControlName="id"
          required/>
      </mat-form-field>
    </div>
    <div class="input-row">
      <mat-form-field fxFlex>
        <mat-label>Name</mat-label>
        <input 
        value="{{data.emp.name}}"
          matInput #name
          placeholder="Email"
          formControlName="name"
          required/>
      </mat-form-field>
    </div>

    <div class="input-row">
      <mat-form-field fxFlex>
        <mat-label>Designation</mat-label>
        <input
        value="{{data.emp.designation}}"
          matInput #designation
          placeholder="designation"
          formControlName="designation"
          required/>
      </mat-form-field>
    </div>

    <br />
    <div class="">
      <button
        mat-raised-button
        type="reset"
        class="btn btn-danger width"
        (click)="close()">   Close</button>&nbsp;&nbsp;
      <button
        mat-raised-button
        [disabled]="!f.valid"
        (click)="update(id.value,name.value,designation.value)"
        right
        class="btn btn-success width right"
        type="submit">
        Update
      </button>
    </div>
  </form>
  <br />

</div>

员工.component.ts

import { Component, OnInit, Inject } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { HomeService } from "../home/home.service";


export interface employee{
  designation: string;
  id:number;
  name:string;
}
@Component({
  selector: "app-employee",
  templateUrl: "./employee.component.html",
  styleUrls: ["./employee.component.css"]
})
export class EmployeeComponent implements OnInit {
  editForm: FormGroup;

  constructor(
    public dialogRef: MatDialogRef<EmployeeComponent>,
    @Inject(MAT_DIALOG_DATA) public data: employee[],
    private service: HomeService
  ) {}

  ngOnInit() {
    this.editForm = new FormGroup({
      id: new FormControl({ disabled: true }, Validators.required),
      name: new FormControl("", [
        Validators.required,
        Validators.pattern(
          /^[A-Za-z]{1,16}([ ]?[a-zA-Z]{0,16})([ ]?[a-zA-Z]{0,16})$/
        )
      ]),
      designation: new FormControl("", [
        Validators.required,
        Validators.pattern(
          /^[A-Za-z]{1,16}([ ]?[a-zA-Z]{0,16})([ ]?[a-zA-Z]{0,16})$/
        )
      ])
    });
    console.log(this.data);
  }

  update(id, name, designation) {
    console.log(this.editForm.value);
    this.service.updateEmployee(id, name, designation).subscribe(
      data => {
        this.dialogRef.close();
        console.log(data);
        console.log("done");
      },
      err => {
        console.log(err);
      }
    );
  }
  close() {
    this.dialogRef.close();
  }
}

Mat Dialog 数据来自

Home.component.ts

 onEdit(emp) {
    console.log("row clicked");
    console.log(emp);
    this.employee1 = emp;
    console.log("clicked");
    const dialogRef = this.dialog.open(EmployeeComponent, {
      width: "430px",
      data: { emp: this.employee1 }
    });

使用 ng serve 但使用 cmd ng serve 时一切正常 --prod 显示错误。

我在 employee.component.ts 中试过这个 id:员工[]; 它不工作

【问题讨论】:

  • 你有没有 console.log 'emp'。它只是一个对象或对象数组。
  • 对象数组
  • console.log(emp) 将输出为 {emp: {…}} emp: designation: "anilreddy" id: 4 name: "anilreddya" proto:对象 proto:对象
  • 似乎是它的对象,而不是“对象数组”。声明员工1:员工;
  • 它不工作

标签: angular data-binding angular6 angular-material2


【解决方案1】:

改变这一行:

@Inject(MAT_DIALOG_DATA) public data: employee[],

@Inject(MAT_DIALOG_DATA) public data: any,

因为数据不是员工数组。在您的情况下,它是一个具有员工数组或员工对象的对象。明天你可以在这个数据变量中传递更多的对象。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 2019-06-16
  • 2019-02-01
  • 2018-11-30
  • 2018-11-25
  • 1970-01-01
  • 2018-11-04
相关资源
最近更新 更多