使用
在entity文件中给需要暴露的属性添加@Expose() @Exclude()
例一
You can expose what your getter or method return by setting a @Expose() decorator to those getters or methods
通过为getter或方法设置@expose()
修饰符,可以公开getter或方法返回的内容
import {
Expose } from "class-transformer"
export class User {
id: number;
firstName: string;
lastName: string;
password: string;
@Expose()
get name() {
return this.firstName + " " + this.lastName;
}
@Expose()
getFullName() {
return this.firstName + " " + this.lastName;
}
}
例二
Exposing properties with different names
If you want to expose some of properties with a different name, you can do it by specifying a name option to @Expose decorator
如果要使用不同的名称公开某些属性,可以通过向@Expose decorator指定一个name选项来实现
import {
Expose } from "class-transformer"
export class User {
@Expose({
name: "uid"}) // 使用uid导出id字段
id: number;
firstName: string;
lastName: string;
@Expose({
name: "secretKey" })
password