【问题标题】:How to refactor this code to CommonJS module syntax?如何将此代码重构为 CommonJS 模块语法?
【发布时间】:2019-11-08 01:53:51
【问题描述】:

正如对this 问题的回答所建议的那样,错误

“未扩展的令牌导出”

是因为下面给出的代码是 es6 语法,但是 Node JS 使用了通用的 JS 模块。

const SidebarMenu = require('./components/SidebarMenu.vue')

    export default {
      install (Vue) {
        Vue.component('sidebar-menu', SidebarMenu)
      }
    }

    export { SidebarMenu }

请帮我把它转换成 commonJS 模块。 我试过module.exports如下

  const SidebarMenu = require('./components/SidebarMenu.vue')

  install = function (Vue) {
    Vue.component('sidebar-menu', SidebarMenu)
  }


module.exports = { SidebarMenu, install }

但它也不起作用。我收到一个错误,提示找不到组件SidebarMenu。

【问题讨论】:

  • 尚不清楚您需要此导出做什么(例如,导入期望什么)。到目前为止,它看起来像是一个 XY 问题。你能解释一下你实际上想要做什么吗?你的 SidebarMenu 应该是 Vue 组件吗?

标签: javascript vue.js ecmascript-6 commonjs


【解决方案1】:

为什么要导出 require 语句。您不能导出 require 语句。 您的函数是否使用 sidebarmenu ,它将与它一起导出。因此无需在您的 module.exports 中执行此操作。

只做module.exports{install},没有侧边栏菜单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多