From b9c752f8c815351559ceb18045d6a4cb0ca9dbe7 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 1 Jul 2024 10:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E8=B7=AF=E7=94=B1=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/router.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/middleware/router.js b/middleware/router.js index b97beb4..97849d6 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -10,10 +10,12 @@ export function createRouter() { return function (req, res, next) { let debug = this.get('debug') let dynamic = this.get('dynamic') + let _ctrl = dynamic ? 'index' : req.controller + let _Module = this.$load(_ctrl) // 1. 先判断控制器是否存在 - if (!this.$load(dynamic ? 'index' : req.controller)) { - return res.error(`Controller [${req.controller}] not found`, 404) + if (!_Module) { + return res.error(`Controller [${_ctrl}] not found`, 404) } // 2. 默认二级路由为index @@ -22,7 +24,7 @@ export function createRouter() { } // 3. 实例化控制器 - this.$load(dynamic ? 'index' : req.controller) + _Module .then(async ModuleController => { let ctrol, route, act let err = '' @@ -61,7 +63,7 @@ export function createRouter() { } } } else { - err = new Error(`Controller [${req.controller}] load error`) + err = new Error(`Controller [${_ctrl}] load error`) err.status = 500 }