gpt4 book ai didi

nestjs - 是否可以将参数传递给nestjs防护程序?

转载 作者:行者123 更新时间:2023-12-03 15:34:56 24 4
gpt4 key购买 nike

我正试图提出一个可重用的后卫,看起来我需要将字符串参数传递给后卫。在nestjs中可以实现吗?

最佳答案

听起来您正在寻找使用mixin,该函数返回一个类。我不确定您要传递哪种参数,但是这个想法是

export const RoleGuard = (role: string) => {
class RoleGuardMixing implemenets CanActivate {
canActivate(context: ExecutionContext) {
// do something with context and role
return true;
}
}

const guard = mixin(RoleGuardMixin);
return guard;
}
mixin作为函数从 @nestjs/common导入,并且是将 @Injectable()装饰器应用于类的包装器函数。

现在要使用守护程序,您需要执行类似 @UseGuards(RoleGuard('admin'))的操作

关于nestjs - 是否可以将参数传递给nestjs防护程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62046413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com