angular - 角度模型中的对象声明/关系
问题描述
是否可以做这样的事情我想在我的模型中声明一个对象我尝试的第一个是
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;
}
解决方案
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")];
}
推荐阅读
- javascript - 每个用户角色的动态菜单 URL
- c# - 如何让用户从另一个表中输入字段并将主键存储到视图所属的表中?[ASP.NET MVC EF6 代码优先]
- sql - 如何将查询保存在已创建的表中?
- eloquent - 无法让 Laravel OrderBy 日期在 Eloquent 语句中工作
- file - 如何从红移的选择查询中将数据加载到本地 csv 文件中,并自动执行日常调度过程。?
- visual-studio - 突出显示当前行号。[视觉工作室 2019]
- c# - 如何从其他局部视图调用局部视图
- bash - 有没有办法让每一行都打印为 make 处理 Makefile?(喜欢在 bash 脚本中使用 -x)
- sql - 我想在不同的查询中使用动态结果
- c - Linux timer_settime() 产生分段错误