【问题标题】:Call function of a dynamically loaded instance of a es6 class调用 es6 类的动态加载实例的函数
【发布时间】:2021-05-30 11:40:30
【问题描述】:

我正在尝试使用 webpack 动态导入“SampleModuleController”类并调用其成员函数,如下所示。

sampleModule.js

  class SampleModuleController{
    
        getSampleModuleSheet()
        {
            console.log("getSampleModuleSheet");
        }
    
    }
    export let sampleModuleController = new SampleModuleController();

在另一个类文件中...

async clicked(){
    const getView = await import('../../controllers/sampleModule');
    console.log("getView--",getView);
    getView.getSampleModuleSheet();
}

但它给了我以下日志和错误

【问题讨论】:

  • 检查async clicked(){ const {sammpleModuleController} = await import('../../controllers/sampleModule'); console.log("getView--",getView); sampleModuleController.getSampleModuleSheet(); }
  • @me.nkr 请给它作为答案。我会把它标记为正确的
  • 如果你想创建一个单例,不要使用class。只需写export function getSampleModuleSheet() { … },您的代码就可以工作。

标签: webpack es6-modules dynamic-import


【解决方案1】:
    async clicked() {
      const {sammpleModuleController: getView} = await import ('../../controllers/sampleModule');
      console.log("getView--", getView);
      getView.getSampleModuleSheet();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2017-08-11
    • 2020-07-22
    • 2023-03-05
    • 2018-07-31
    • 2012-04-12
    • 2016-09-16
    • 2013-04-12
    相关资源
    最近更新 更多