
VUE项目中引入JS文件的几种方法
在开发Vue项目的时候,有时需要使用一些非ES6格式的没有export的js库,可以有如下方法实现:
1.在index.html页面使用script标签引入
当然也可以使用cdn的地址。这样引入后的内容是全局的,可以在所有地方使用。
立即学习“前端免费学习笔记(深入)”;
<!DOCTYPE html>
<html>
<head>
<title>Map</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="./static/img/favicon.ico"/>
<script src='./static/libs/three/three.min.js'></script>
<script src="./static/libs/three/GLTFLoader.js"></script>
</head>
<body>
<p id="app"></p>
<!-- built files will be auto injected -->
</body></html>2.在main.js中使用window.moduleName 使用
也可以放入Vue.prototype中,这样组件内都可以使用。
var THREE = window.THREEvar GLTFLoader = THREE.GLTFLoader Vue.prototype.THREE = THREE
3.手动添加export
为js库中需要使用的方法放入export default { /**要导出的方法**/},然后通过import {*} from 使用
在JS库中:
function realconsole(){
alert("hello world!"); }
export {
realconsole }在需要使用JS库的组件中:
import realconsole from './xxx'
4. 使用import方式,把需要的js库中的方法挂载到全局
如下:
import '@static/libs/GLTFLoader' // 可以从全局获取导入的方法 let GLTFLoader = THREE.GLTFLoader
相关推荐:2020年前端vue面试题大汇总(附答案)vue教程推荐:2020最新的5个vue.js视频教程精选
更多编程相关知识,请访问:编程入门!!
以上就是vue引入js文件有哪几种方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号