如何在 es6 中转换 require("./handlers/event.js")(client)?

Posted

技术标签:

【中文标题】如何在 es6 中转换 require("./handlers/event.js")(client)?【英文标题】:How to convert require( "./handlers/event.js")(client) in es6? 【发布时间】:2022-01-20 14:13:52 【问题描述】:

我想将require( "./handlers/event.js")(client) 转换为 es6,但找不到怎么做。

【问题讨论】:

这能回答你的问题吗? The difference between "require(x)" and "import x" 【参考方案1】:

该行正在从一个看起来像这样的模块导入

module.exports = function(client) 
  this.newClient = function() 
    var x = client()
    // ...
  

在 ES6 中看起来像这样

export default function(client) 
  this.newClient = function() 
    var x = client()
    // ...
  
  return this

对于以 ES6 方式导入,应该类似于

import FOO from "./handlers/event"

然后你可以做类似的事情

const  newClient  = FOO(new Client())
const p = newClient() // or anything you want to do with it

【讨论】:

以上是关于如何在 es6 中转换 require("./handlers/event.js")(client)?的主要内容,如果未能解决你的问题,请参考以下文章