我是靠谱客的博主 俊秀汽车,这篇文章主要介绍Vue 如何import服务器上的js配置文件,现在分享给大家,希望可以做个参考。

背景

项目中有一个本地配置文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
// src/image-position.js export default { label: '首页', value: 'home', data: [ { label: '轮播', value: 'carousel' } ] }

如何引用一个本地文件大家都知道:

复制代码
1
import ImagePosition from './image-position.js'

现在需要把image-position.js文件丢到服务器上去,得到它的链接:

xxx.com/static/imag…

这个时候你直接引用文件地址自然是行不通的。

复制代码
1
2
3
import ImagePosition from 'https://xxx.com/static/image-position.js' // ERROR This dependency was not found

实现

首先对image-position.js做一点小改造,暴露一个全局对象ImagePosition

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 改造后的image-position.js (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.ImagePosition = factory()); }(this, (function () { 'use strict'; return { label: '首页', value: 'home', data: [ { label: '轮播', value: 'carousel' } ] }; })));

在vue.config.js文件里添加externals。

复制代码
1
2
3
4
5
6
7
module.exports = { configureWebpack: config => { config.externals = { 'image-position': 'ImagePosition' } } }

index.html 区分环境并引入js文件。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> <% if (NODE_ENV == 'production') { %> <script src="http://xxx.com/static/image-position.js"></script> <% } else { %> <script src="http://test.xxx.com/static/image-position.js"></script> <% } %> </body> </html>

结束上面的步骤后就可以愉快的引用image-position.js文件了。

复制代码
1
2
3
import ImagePosition from 'image-position' console.log(ImagePosition) // {label: '首页',value: 'home',data: [{label: '轮播', value: 'carousel'}]}

补充vue-cli2.0下如何配置

复制代码
1
2
3
4
5
6
7
// build/webpack.base.conf.js module.exports = { externals: { // 新增 'image-position': 'ImagePosition' } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> <% if (process.env == 'production') { %> <script src="http://xxx.com/static/image-position.js"></script> <% } else { %> <script src="http://test.xxx.com/static/image-position.js"></script> <% } %> </body> </html>

总结

在Vue项目的打包体积优化中,cdn加速是常用的一种手段,上面其实就是cdn加速的实现内容,把第三方库通过script标签引入,大大减少打包的vendor.js文件大小。

当我们想把本地文件放到服务器远程化时,关键在于实现步骤的第一步,其他的内容跟配置cdn加速的过程是一样的。

以上就是Vue 如何import服务器上的js配置文件的详细内容,更多关于Vue import js配置文件的资料请关注靠谱客其它相关文章!

最后

以上就是俊秀汽车最近收集整理的关于Vue 如何import服务器上的js配置文件的全部内容,更多相关Vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(153)

评论列表共有 0 条评论

立即
投稿
返回
顶部