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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM