代码之家  ›  专栏  ›  技术社区  ›  Anil Kumar Reddy A

类型“employee[]-Angular 6 ng serve--prod”上不存在属性“emp”

  •  0
  • Anil Kumar Reddy A  · 技术社区  · 7 年前

    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>
    

    Empoyee.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对话框数据来自

    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时 --戳它的显示错误。

    id:雇员[]; 它不起作用

    1 回复  |  直到 7 年前
        1
  •  5
  •   Vikram Jain    7 年前

    更改此行:

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

    到

    @Inject(MAT_DIALOG_DATA) public data: any,
    

    因为数据不是雇员数组。它是一个在您的案例中包含employee数组或employee对象的对象。明天您可以在此数据变量中传递更多对象。

    推荐文章