-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
希望 extends继承时可以继承装饰器 #2364
Comments
基类上不建议添加装饰器 |
我主要想实现分模块, V1模块
期望的访问地址为: V2模块
期望的访问地址为: 思路我看官方的
改为获取到controllerOption后递归拿下原型的controllerOption,然后加个@controller入参控制下是否合并祖先的属性,进行controllerOption合并后再注册,应该可以实现。 不知道官方有没有更好的实现方式和建议 |
可以试试把不同版本的中间件合并为一个入口,通过 ctx.path 来调用不同版本(中间件)逻辑 |
那样只解决了中间件的问题,前缀问题依然没有解决,而且每次请求都会进行判断,不像注册时只会执行一次。我还在自己封个装饰器,动态注册路由吧。 |
import {
Provide,
Get,
Configuration,
App,
Inject,
Middleware,
Init,
} from '@midwayjs/decorator';
import * as koa from '@midwayjs/koa';
import { join } from 'path';
import { MidwayWebRouterService } from '@midwayjs/core';
@Provide()
export class HomeController {
@Inject()
ctx;
@Get('/')
async home(): Promise<string> {
return 'Hello Midwayjs!' + this.ctx.user;
}
}
@Middleware()
export class CustomMiddleware {
resolve() {
return async (ctx, next) => {
ctx.user = 'v1';
await next();
};
}
}
@Middleware()
export class CustomMiddleware2 {
resolve() {
return async (ctx, next) => {
ctx.user = 'v2';
await next();
};
}
}
@Configuration({
imports: [koa],
importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle {
@App()
app: koa.Application;
@Inject()
webRouterService: MidwayWebRouterService;
@Init()
async init() {
this.webRouterService.addController(HomeController, {
prefix: '/v1',
routerOptions: {
middleware: [CustomMiddleware],
},
});
this.webRouterService.addController(HomeController, {
prefix: '/v2',
routerOptions: {
middleware: [CustomMiddleware2],
},
});
}
} |
@czy88840616 这种方式是否合适,如果感觉合适我可以完善下提个PR |
暂时不建议动原型链,原型链会有不少悖论和延展的问题,比如重名的装饰器是组合还是继承,配置如何处理,swagger 扩展等等。 |
好的,希望官方能规划出更方便的分模化方案。 |
比如
这时候路由
/api/user
并不存在。经测试在
nestjs
中这样写是可以访问/api/user
的。The text was updated successfully, but these errors were encountered: