# JavaScript 如何按需加载模块

URL: https://caijiao.org/how-to/javascript/dynamic-import
Source: docs/how-to/javascript/dynamic-import.md
Description: JavaScript 如何按需加载模块：用 import() 返回 Promise，在需要功能时再加载模块。 示例可直接复制到浏览器或项目代码中，并附相关教程、工具入口。

## 快速答案

用 import() 返回 Promise，在需要功能时再加载模块。

```js
const { renderChart } = await import('./chart.js');
renderChart(data);
```

## 使用要点

- 异步示例要在调用边界捕获错误，避免未处理的 Promise rejection。
- 重试和动态导入都要限制触发条件，防止失败时重复打满接口或资源。

## 相关内容

- [JavaScript / TypeScript 问题](/how-to/javascript/)
- [JavaScript 教程](/javascript/)
- [TypeScript 教程](/ts/)
- [JSON Formatter](/tool/json-formatter)
- [JSON Validator](/tool/json-validator)
