如何在Webpack应用程序的Typescript中使用动态导入 [英] How to use dynamic import in typescript with webpack application

查看:83
本文介绍了如何在Webpack应用程序的Typescript中使用动态导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据按钮的点击动态导入了我的一些模块,我已经像下面那样使用了

I have import some of my modules dynamically based on button click, i have used like below"

import { Button } from '@syncfusion/ej2-buttons';

// Initialize Button component.
let button: Button = new Button({ content: 'Button' });

// Render initialized Button.
button.appendTo('#element');

document.getElementById('element').addEventListener("click", function(){
  check();
});

async function check() {
import("@syncfusion/ej2-grids").then((Grid) => {
  debugger
  console.log(Grid);
});
}

我的webpack配置文件:

my webpack config file:

const path = require("path");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

module.exports = {
  mode: "development",
  entry: {
    "src/app/bundle": "./src/app/index.ts"
  },
  output: {
    publicPath: "/js/",
    filename: "src/app/bundle.js"
  },
  devtool: "none",
  module: {
    rules: [
      {
        loader: "ts-loader",
        exclude: /node_modules/
      }
    ]
  },

  resolve: {
    extensions: [".ts", ".js"]
  }
};

当我编译并运行我的应用程序时,它抛出以下错误

when i compile and run my application it throws below error

当前行为是什么?

What is the current behavior?

抛出以下错误

如果当前行为是错误,请提供重现步骤.

If the current behavior is a bug, please provide the steps to reproduce.

clone sample - https://github.com/kumaresan-subramani/dynamic-import/blob/master/webpack.config.js
run npm i
gulp compile
gulp start

然后单击浏览器中的按钮,您将在控制台中看到报告的错误.

Then click button in browser, you will see reported error in my console.

预期的行为是什么?

What is the expected behavior?

动态模块应正确加载

其他相关信息:

Other relevant information:

"typescript": "3.7.5",
"uglifyjs-webpack-plugin": "^2.0.1",
"webpack": "^4.29.6",
"webpack-cli": "*",
"webpack-stream": "^5.2.1"
Node.js version: 10.15.1
Operating System: windows 10

推荐答案

整个逻辑对我来说很奇怪. 如果您使用模块,只需将其导入,然后在特定条件下或触发事件后使用它

The whole logic seems weird for me. If you use a module just import it and then use it under certain conditions or after event is triggered

这篇关于如何在Webpack应用程序的Typescript中使用动态导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆