autowired 自动初始化
2025/4/16大约 2 分钟
autowired 自动初始化
说明
autowired 用于自动初始化单个嵌套模型对象。当 DTO 中没有对应数据时,会自动创建一个空的模型实例。
适用范围
autowired 仅对单个嵌套对象有效,不适用于:
- 数组类型(请使用
default: () => []) - 普通类型(请使用
default)
配置
| 参数 | 类型 | 说明 |
|---|---|---|
autowired | boolean | 是否自动初始化,仅对单个嵌套对象有效 |
model | Class | () => Class | 嵌套模型类型,必须配合 autowired 使用 |
使用场景
- 表单编辑时,需要确保嵌套对象存在
- 避免访问嵌套属性时出现
undefined错误
正确用法
class Order extends ModelBase {
// ✅ 正确:单个嵌套对象使用 autowired
@Column({ model: Address, autowired: true })
address?: Address
// ✅ 正确:数组使用 default
@Column({ model: OrderItem, default: () => [] })
items?: OrderItem[]
// ❌ 错误:autowired 对数组无效
@Column({ model: OrderItem, autowired: true })
items?: OrderItem[] // 结果是 undefined,不是 []
}对比 default
// autowired:自动创建空模型实例(仅对象)
@Column({ model: Address, autowired: true })
address?: Address
// 等效于
@Column({ model: Address, default: () => new Address() })
address?: Address
// default 更灵活:可以设置详细的默认值
@Column({ model: Address, default: () => new Address({ city: '北京' }) })
address?: Address
// 数组只能用 default
@Column({ model: OrderItem, default: () => [] })
items?: OrderItem[]案例
对象模型(autowired 有效)
模型
class TestItem extends ModelBase {
@Column()
public message?: string
}
class Test extends ModelBase {
// 对象初始化
@Column({
model: () => TestItem,
autowired: true,
})
public initObject?: TestItem
// 对象未初始化
@Column({
model: () => TestItem,
autowired: false,
})
public noInitObject?: TestItem
}实例初始化
const test = new Test()打印日志
console.log(test.initObject)
// { message: undefined }
console.log(test.noInitObject)
// undefined数组模型(使用 default)
说明
数组类型不能使用 autowired,必须使用 default: () => []。
模型
class TestItem extends ModelBase {
@Column()
public message?: string
}
class Test extends ModelBase {
// 数组初始化
@Column({
model: () => TestItem,
default: () => [],
})
public initArray?: TestItem[]
// 数组未进行初始化
@Column({
model: () => TestItem,
})
public noInitArray?: TestItem[]
}实例初始化
const test = new Test()打印日志
console.log(test.initArray)
// []
console.log(test.noInitArray)
// undefined@tab 普通类型(autowired 无效)
注意
autowired 对普通类型完全无效,以下示例中所有值都是 undefined。
模型
class Test extends ModelBase {
// 数字类型
@Column({ autowired: true })
public num?: number
// 字符串类型
@Column({ autowired: true })
public str?: string
// 布尔类型
@Column({ autowired: true })
public bool?: boolean
}实例初始化
const test = new Test()打印日志
console.log(test.num)
// undefined
console.log(test.str)
// undefined
console.log(test.bool)
// undefined:::
注意事项
autowired仅对单个嵌套对象有效,必须配合model使用- 数组初始化:使用
default: () => [],不要用autowired - 普通类型初始化:使用
default,参考default - 如果需要设置更复杂的默认值,建议使用
default函数
