您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue引入js文件有哪几种方法

2024/2/27 22:45:58发布10次查看
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.gltfloadervue.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文件有哪几种方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product