简体   繁体   English

如何在带有 webpack 应用程序的打字稿中使用动态导入

[英]How to use dynamic import in typescript with webpack application

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); }); }

my webpack config file:我的 webpack 配置文件:

 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?目前的行为是什么?

Throws below error抛出以下错误错误信息

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?什么是预期行为?

Dynamic module should load properly动态模块应该正确加载

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如果您使用模块,只需导入它,然后在特定条件下或触发事件后使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM