使用 unpkg 导入 three.js 时,为什么在 main.js 中无法识别 THREE?
2024-11-21 22:42:27
0浏览
收藏
文章小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《使用 unpkg 导入 three.js 时,为什么在 main.js 中无法识别 THREE? 》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!

通过 unpkg 引入 three.js
您希望通过 unpkg 在没有任何前端框架的情况下导入 three.js。然而,在您的代码中,您在 main.js 中无法识别 THREE。
出现此错误是因为您尚未在 index.html 中正确导入 THREE 模块。要解决此问题,请进行以下更改:
使用 importmap 引入 three.js 模块:
index.html
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.155.0/build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
console.log(THREE);
</script>或者,直接在
