在 Next JS 中导入动态打字稿文件

Ask*_*ing 0 javascript reactjs next.js

根据 NextJs 文档,我们可以对 javascript 文件使用动态导入const DynamicComponent = dynamic(() => import('../components/hello'))。 对于这样的文件
使用动态导入也是正确的:.tsx

const MyComponent = dynamic(() => import('main/my-file.tsx'), {
  ssr: false,
});
Run Code Online (Sandbox Code Playgroud)

?或者我们可以仅动态导入.js文件?

Fer*_*tel 5

我想你可以ts通过一些解决方法在 nextjs 中动态导入文件。

import dynamic from "next/dynamic";
...
...
const Button = dynamic(() => import("../components/Button").then(module => module.default));
Run Code Online (Sandbox Code Playgroud)

如果您想进一步阅读https://github.com/vercel/next.js/issues/4515