我有一个组件,我正在使用 nextjs 中的 dynamic
动态导入它。而且我也想根据我在代码中设置的标志(下面代码块中的 loadWirelessBundle 标志)下载包。所以它在下载包和呈现组件方面工作正常但它不做组件的服务器端呈现(当 loadWirelessBundle 为真时)即使我正在传递 ssr = true。有谁知道为什么它不做服务端渲染?
const WirelessPrepaid = loadWirelessBundle ? dynamic(() => import("../wireless-prepaid"), { ssr: true }) : () => null;
在理解 nextjs 的动态导入方面,我是否遗漏了什么?有人可以帮忙吗?谢谢!
最佳答案
您需要在顶层进行导入
// this goes at the top level of the module
const WirelessPrepaidDynamicComponent = dynamic(() => import("../wireless-prepaid"));
// this can be placed wherever you need it
const WirelessPrepaid = loadWirelessBundle ? WirelessPrepaidDynamicComponent : () => null;
关于reactjs - 使用下一个 js 动态导入有条件地导入模块,SSR 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66791922/