Import-via-modules.html 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>通过模块来引入([name])</h1>
  12. <p>
  13. 虽然通过script标签来引入three.js是一个能够快速起步、快速运行的方式,但这种方式对于一些具有较长生命周期的项目来说是有一些缺点。比如:
  14. <ul>
  15. <li>你必须手动获得并在你的项目源代码中包含这个库的一个拷贝</li>
  16. <li>更新库的版本是一个手动操作的过程</li>
  17. <li>在检查新版本的库时,你的版本差异对比将会被许多行的构建文件给弄乱。</li>
  18. </ul>
  19. </p>
  20. <p>使用像npm这样的依赖包管理器,你只需在你的机器上下载并导入你所需要的版本的库就很好地避免这些需要注意的问题。</p>
  21. <h2>通过npm来安装</h2>
  22. <p>Three.js目前已经作为一个npm模块来进行了发布,详情请参阅:[link:https://www.npmjs.com/package/three npm]。这意味着你只需运行"npm install three"就可以使你的项目包含three.js库。</p>
  23. <h2>导入这个模块</h2>
  24. <p>假设你正在使用[link:https://webpack.github.io/ Webpack]或者[link:https://github.com/substack/node-browserify Browserify]等允许你“通过打包所有依赖,来在浏览器中使用require('modules')”的打包工具对你的文件进行打包。</p>
  25. <p>你现在可以在你的源代码中引入模块,并继续像往常一样使用这个库。
  26. </p>
  27. <code>
  28. var THREE = require('three');
  29. var scene = new THREE.Scene();
  30. ...
  31. </code>
  32. <p>
  33. 你也可以使用[link:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import ES6 import](在ES6标准中新增的import语句)
  34. </p>
  35. <code>
  36. import * as THREE from 'three';
  37. const scene = new THREE.Scene();
  38. ...
  39. </code>
  40. <p>
  41. 或者,如果你希望只导入three.js库中的特定部分,例如Scene:
  42. </p>
  43. <code>
  44. import { Scene } from 'three';
  45. const scene = new Scene();
  46. ...
  47. </code>
  48. <h2>可引入的示例</h2>
  49. <p>
  50. The core of three.js is focused on the most important components of a 3D engine. Many other components like loaders or controls are part of the
  51. examples directory. three.js ensures that these files are kept in sync with the core but users have to import them separately if they are required
  52. for a project. You can find them in the [link:https://github.com/mrdoob/three.js/tree/master/examples/jsm examples/jsm] directory. If you install three.js
  53. via npm, import example files like so:
  54. </p>
  55. <code>
  56. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  57. </code>
  58. <p>
  59. 请注意:当你在使用来自示例(examples)文件夹中的代码时,其中的所有文件和你的three.js主文件版本相匹配是很重要的。
  60. 比如说,three.js的R103版本不能够接受和来自R96版本的*GLTFLoader*和*OrbitControls*一起使用。
  61. </p>
  62. </body>
  63. </html>