gpt4 book ai didi

node.js - 导出变量 'router' 具有或正在使用来自外部模块的名称 'Router' 但无法命名

转载 作者:搜寻专家 更新时间:2023-10-30 21:15:32 39 4
gpt4 key购买 nike

在直接使用 Typescript 2 和 @types/express@4.0.35 定义导出路由器时,在多个文件中定义嵌套路由器的常见构造中,代码抛出

error TS4023: Exported variable 'router' has or is using name 'Router' from external module 
"[...]/node_modules/@types/express-serve-static-core/index" but cannot be named.

示例取自 basarat's answer

import * as express from "express";

// import sub-routers
import * as adminRouter from "./admin/admin";
import * as productRouter from "./products/products";

let router = express.Router();

// mount express paths, any addition middleware can be added as well.
// ex. router.use('/pathway', middleware_function, sub-router);

router.use('/products', productRouter);
router.use('/admin', adminRouter);

// Export the router
export = router;

最佳答案

解决方案是显式注释路由器变量 ( TypeScript/issues/5711#issuecomment-161194656 )。

以下代码将起作用(注意 let router: express.Router = express.Router())

import * as express from "express";

import * as adminRouter from "./admin/admin";
import * as productRouter from "./products/products";

let router: express.Router = express.Router();

router.use('/products', productRouter);
router.use('/admin', adminRouter);

// Export the router
export = router;

关于node.js - 导出变量 'router' 具有或正在使用来自外部模块的名称 'Router' 但无法命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41847314/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com