首页 > 解决方案 > 角度模型中的对象声明/关系

问题描述

是否可以做这样的事情我想在我的模型中声明一个对象我尝试的第一个是

export class Employee{
    emp_id: number;
    emp_fname: string;
    emp_lname: string;
    emp_birth: string;
    emp_status: string;
    emp_photo: string;
    emp_department: string; 
    department: Array<object> = 
    [{
        dept_id: number;
        dept_name: string;
    }];

第二个是这样的

import { Department } from "./department.model";

export class Employee{
  constructor(department: Department){};
    emp_id: number;
    emp_fname: string;
    emp_lname: string;
    emp_birth: string;
    emp_status: string;
    emp_photo: string;
    emp_department: string; 
    department: department;

}

两者都在我的终端中返回错误:第一个返回 expects ,但是当我这样做时,它说我应该使用 eg:number 作为类型而不是值。

然后第二个返回错误

找不到名称“部门”。

我的部门模型

export class Department {
    dept_id: number;
    dept_name: string;
}

标签: angulartypescriptmodels

解决方案


export class Department {
    dept_id: number;
    dept_name: string;
    constructor(dept_id, dept_name){
        this.dept_id = dept_id;
        this.dept_name = dept_name;
    }
}

export class Employee{
    emp_id: number;
    emp_fname: string;
    emp_lname: string;
    emp_birth: string;
    emp_status: string;
    emp_photo: string;
    emp_department: string; 
    department: Department[] = [new Department(1, "dept_name1"), new Department(2, "dept_name2")];
}

推荐阅读