nest transformer Expose Exclude 练习

使用

在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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值