这篇文章主要介绍了Vue 如何import服务器上的js配置文件,帮助大家更好的理解和学习使用vue框架,感兴趣的朋友可以了解下
正文
Vue 如何import服务器上的js配置文件
这篇文章主要介绍了Vue 如何import服务器上的js配置文件,帮助大家更好的理解和学习使用vue框架,感兴趣的朋友可以了解下
背景
项目中有一个本地配置文件:
|
1
2
3
4
5
6
7
8
9
10
11
|
// src/image-position.jsexport 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.jsmodule.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配置文件的资料请关注米米素材网其它相关文章!
原文链接:https://juejin.cn/post/6948312676413997093

发表评论