【问题标题】:Cannot import JS file in ViteVite 无法导入 JS 文件
【发布时间】:2022-11-14 16:36:08
【问题描述】:

我从不使用 Vite,我开始在新的 Rails 应用程序中使用它。

我正在尝试在我的应用程序中使用基于引导程序的管理主题,并且我正在使用 Vite for JS。

我有这条线的入口点

import '../admin/vendor/metronic/components/_init'

在 init.js 文件中,我对主题的 js 组件进行了初始化

var KTComponents = function () {
    // Public methods
    return {
        init: function () {
            KTApp.init();
                KTDrawer.init();
                KTMenu.init();
                KTScroll.init();
                KTSticky.init();
                KTSwapper.init();
                KTToggle.init();
                KTScrolltop.init();
                KTDialer.init();
                KTImageInput.init();
                KTPasswordMeter.init();
        }
    }
}();

// On document ready
if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", function() {
        KTComponents.init();
    });
 } else {
    KTComponents.init();
 }

 // Init page loader
window.addEventListener("load", function() {
    KTApp.initPageLoader();
});

// Declare KTApp for Webpack support
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    window.KTComponents = module.exports = KTComponents;
}

当然我有这个错误:

Uncaught ReferenceError: KTApp is not defined

为了解决这个问题,我尝试添加

import KTApp from './app';

但我现在有这个错误

Uncaught SyntaxError: The requested module '/vite-dev/admin/vendor/metronic/components/app.js' does not provide an export named 'default' (at _init.js?t=1668157592237:6:8)

这是 app.js 文件

“使用严格”;

// Class definition
var KTApp = function () {
  ...
}

// Declare KTApp for Webpack support
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = KTApp;
}

【问题讨论】:

    标签: javascript vite


    【解决方案1】:

    您没有在带有声明的 app.js 文件中导出 KTApp。这就是为什么错误说“不提供名为'default'的导出”。试试这个export default KTApp = function(){}

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 2021-09-21
      • 2021-05-15
      • 2022-08-16
      • 2021-02-20
      • 1970-01-01
      • 2021-07-23
      • 2022-12-15
      • 2018-02-19
      相关资源
      最近更新 更多