nodejs 环境下如何实现装饰器
在nodejs 环境下如何实现装饰器,有了解的大佬指点下哈,想实现如下功能。
class Boy {
@run
speak() {
console.log("I can speak");
}
}
function run() {
console.log("I can run");
}
let tj = new Boy();
tj.speak();
6 回复
babel、typescript
@yuu2lee4 我弄好了配置文件 .babelrc
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose" : true }]
]
但是要如何使用呢?
webpack 先编译下,再node执行
@weiketa 感谢,我试试
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class RolesGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
return true;
}
}
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
console.log('Before...');
const now = Date.now();
return next
.handle()
.pipe(
tap(() => console.log(`After... ${Date.now() - now}ms`)),
);
}
}
@zuohuadong nestjs 框架我已经在使用了(非常好),现在还考虑的是如何给 koa2 实现装饰器的功能