问个 egg 加载 controller 的 this 指向问题
ret[key] = methodToMiddleware(Controller, key);
function methodToMiddleware(Controller, key) {
return function classControllerMiddleware(...args) {
const controller = new Controller(this);
if (!this.app.config.controller || !this.app.config.controller.supportParams) {
args = [this];
}
return utils.callFn(controller[key], args, controller);
};
}
const controller = new Controller(this);
这个 this 怎么会指向 context .
1 回复
找到了。。。
function convertMiddlewares(middlewares, app) {
// ensure controller is resolved
const controller = resolveController(middlewares.pop(), app);
// make middleware support generator function
middlewares = middlewares.map(utils.middleware);
const wrappedController = (ctx, next) => {
return utils.callFn(controller, [ ctx, next ], ctx);
};
return middlewares.concat([ wrappedController ]);
}