程序开发 · 2024年12月26日

使用 unpkg 导入 three.js 时,为什么在 main.js 中无法识别 THREE?

通过 unpkg 引入 three.js

您希望通过 unpkg 在没有任何前端框架的情况下导入 three.js。然而,在您的代码中,您在 mn.js 中无法识别 THREE。

出现此错误是因为您尚未在 index.html 中正确导入 THREE 模块。要解决此问题,请进行以下更改:

使用 importmap 引入 three.js 模块:

index.html

<script type="importmap">
  {
    "imports": {
      "three": "https://unpkg.com/[email protected]/build/three.module.js"
    }
  }
</script>
<script type="module">
  import * as THREE from 'three';
  console.log(THREE);
</script>

登录后复制

或者,直接在 <script> 标签中导入 three.js 模块:</script>

index.html

<script type="module">
  import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js';
  console.log(THREE);
</script>

登录后复制

现在,您可以在 main.js 中通过以下方式访问 THREE:

main.js

console.log("Hello Three.js");
console.log("js" + THREE);
const scene = new THREE.Scene();

登录后复制

以上就是使用 unpkg 导入 three.js 时,在 mn.js 中无法识别 THREE?的详细内容,更多请关注GTHOST其它相关文章!