是否可以动态导入节点模块?

Mel*_* H. 6 javascript ecmascript-6

我想知道是否可以动态导入通过 npm install 安装的模块。

就像是:

import("{ AsyncStorage } from react-native ???").then((module) => {
  // do something with module
});
Run Code Online (Sandbox Code Playgroud)

到目前为止,我发现的所有示例都只是导入自己编写的 ES 模块...

Ang*_*ext 3

你应该这样做

// With normal promises
import('react-native').then(({ AsyncStorage }) => {
 // Do something with the module
})

// Async/await (You must add this code inside of an async function or use ecmascript module code)
const { AsyncStorage } = await import('react-native')
// Do something with the module
Run Code Online (Sandbox Code Playgroud)