[英]Dynamic importing a library next.js
I am trying to import a library dynamically in one of my next.js project.我正在尝试在我的 next.js 项目之一中动态导入库。 When i try to import that in don't get the default export from the library as expected.
当我尝试将其导入时,不会按预期从库中获取默认导出。 Firstly, i tried the next.js way as:
首先,我尝试了 next.js 方式:
import dynamic from "next/dynamic";
const ConnectionReroutePlugin = dynamic(() => import('rete-connection-reroute-plugin'), { ssr: false });
Then i tried the react way as:然后我尝试了反应方式:
const ConnectionReroutePlugin = React.lazy(() => import('rete-connection-reroute-plugin'));
I am seeing the following import error:我看到以下导入错误:
Unhandled Rejection (TypeError): plugin.install is not a function
未处理的拒绝(TypeError):plugin.install 不是函数
Anyone can address, what i am doing wrong and how can i import that?任何人都可以解决,我做错了什么,我该如何导入?
I think the issue is you need to pass import as a loader.我认为问题是您需要将导入作为加载程序传递。 like following
喜欢以下
const ConnectionReroutePlugin = dynamic({
loader: () => import("rete-connection-reroute-plugin"),
ssr: false
});
You can do something like this你可以做这样的事情
var script = document.createElement('script');
script.onload = function () {
// do something here
};
script.src = something;
document.head.appendChild(script);
In order to make a dynamic import of a library, you can do为了使库的动态导入,你可以做
import('rete-connection-reroute-plugin')
.then(({ default: ConnectionReroutePlugin }) => /* do what you want */)
also, async/await syntax can be used.此外,可以使用 async/await 语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.