typescript - 打字稿:将动态数据传递给扩展类
问题描述
我想知道是否有人可以提供帮助?
我有一个需要从 3rdparty 扩展的课程。扩展时我需要动态传递数据 - 因此这是类
class AuthGuardNew extends AuthGuard("Needs changing") {
}
所以我想做的是这样的
new AuthGuardNew("Something different")
在幕后将“不同的东西”传递给扩展类,而不是“需要改变”
如果我使用像这样的构造函数
constructor(type:string) {
}
但是当它已经被扩展时,我怎么能把它传递给 AuthGuard 呢?
有任何想法吗 ?
提前致谢
更新
如果我尝试做
export class AuthGuardNew extends AuthGuard {
constructor(type?: string) {
super(type)
}
我收到一个打字稿错误,说明
Type '(type?: string | undefined) => Type' 不是构造函数类型。
解决方案
长话短说,你不能。AuthGuard
是一个创建类的函数,而不是类。在您知道type
.
相反,您可以创建一个新函数,一旦您知道type
.
const AuthGuardNew = (type?: string) =>
class extends AuthGuard(type || 'Needs changing') {
// You are free to override anything you need, you are defining a class
myExtension(): number {
return 10;
}
}
AuthGuardNew
又是一个创建类而不是类的函数。
const SomethingDifferentAuthGuard = AuthGuardNew('Something different');
// SomethingDifferentAuthGuard is now a class that extends AuthGuard('Something different')
// so we can instantiate and use it as such
console.log(new SomethingDifferentAuthGuard().myExtension())
推荐阅读
- azure-ad-b2c - Azure B2C - PublicClientApplication.AcquireTokenByUsernamePasswordAsync 失败
- anylogic - 我应该如何实现功能来选择我想要的路线?
- excel - 如何修改超链接中的变量?
- c# - 如何将 C# 日期时间值转换为 UTC 时区
- ios - 任务切换器和消息警报中的 ApplicationWillResignActive
- reactjs - "$npm_execpath" 未被识别为内部或外部命令、程序或可执行批处理文件
- javascript - 使用 Node 为 Vue 应用程序提供服务时,构建会导致空的 HTML 页面出现语法错误
- ios - 是否有任何安全的方法可以在运行时删除和重新配置 Firebase 应用程序
- c# - 获取方法输出/引用参数的基本类型
- javascript - 扩展 `Promise` 并更改 `then` 签名