Vue引入ES5的js库

Vue项目有时候需要一些没有使用export的js库,不能通过import * from ./***"引入,那么可以有如下方法如下

1.可以在index.html页面使用script标签引入,当然也可以使用cdn的地址。这样引入后的内容是全局的,可以在所有地方使用

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <title>Map</title>
 5     <meta charset="utf-8">
 6     <meta name="viewport" content="width=device-width,initial-scale=1.0">
 7     <link rel="shortcut icon" type="image/x-icon" href="./static/img/favicon.ico"/>
 8     <script src='./static/libs/three/three.min.js'></script>
 9     <script src="./static/libs/three/GLTFLoader.js"></script>
10   </head>
11   <body>
12     <div id="app"></div>
13     <!-- built files will be auto injected -->
14   </body>
15 </html>

2.在main.js中使用window.moduleName 使用

也可以放入Vue.prototype中,这样组件内都可以使用。

1 var THREE = window.THREE
2 var GLTFLoader = THREE.GLTFLoader
3 Vue.prototype.THREE = THREE 

 3.手动添加export的方式

为js库中需要使用的方法放入export default { /**要导出的方法**/},然后通过import {*} from 使用

4. 直接使用import 'url' 方式,把需要的js库中的方法挂载到全局,如下:

1 import '@static/libs/GLTFLoader'
2 // 可以从全局获取导入的方法
3 let GLTFLoader = THREE.GLTFLoader
原文地址:https://www.cnblogs.com/jyughynj/p/11225398.html