我试过了
const TRTC = dynamic(() => import('trtc-js-sdk').then((module) => module.NamedExport), { ssr: false });
它给出了错误,TRTC.createClient不是构造函数。我也尝试过嵌套动态导入,但随后开始出现以下错误。
x.js
------------------------
const TRTC = dynamic(() => {
import("./test_y"), { ssr: false }
});
--------------------
test_y.js
---------------------
const TRTC = dynamic(() => import('trtc-js-sdk').then((module) => module.NamedExport));
------------------------
TypeError: Cannot read property 'then' of undefined
at LoadableSubscription.load [as _loadFn] (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:22:190)
at LoadableSubscription.retry (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:24:1327)
at new LoadableSubscription (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:24:1237)
at init (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:22:1274)
at flushInitializers (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:24:2654)
at Promise (/Users/xyz/Documents/node_modules/next/dist/next-server/lib/loadable.js:24:2852)
我使用的是Next.js 10,不能升级。如何在不运行服务器的情况下在客户端动态导入sdk?
发布于 2022-11-03 13:43:16
尝尝这个
let TRTC = null;
if (typeof window === 'object') {
TRTC = require('trtc-js-sdk').default;
}
https://stackoverflow.com/questions/74301887
复制相似问题