Vue 如何import服務(wù)器上的js配置文件
背景
項目中有一個本地配置文件:
// src/image-position.js
export default {
label: '首頁',
value: 'home',
data: [
{
label: '輪播',
value: 'carousel'
}
]
}
如何引用一個本地文件大家都知道:
import ImagePosition from './image-position.js'
現(xiàn)在需要把image-position.js文件丟到服務(wù)器上去,得到它的鏈接:
xxx.com/static/imag…
這個時候你直接引用文件地址自然是行不通的。
import ImagePosition from 'https://xxx.com/static/image-position.js' // ERROR This dependency was not found
實現(xiàn)
首先對image-position.js做一點小改造,暴露一個全局對象ImagePosition
// 改造后的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。
module.exports = {
configureWebpack: config => {
config.externals = {
'image-position': 'ImagePosition'
}
}
}
index.html 區(qū)分環(huán)境并引入js文件。
// 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>
結(jié)束上面的步驟后就可以愉快的引用image-position.js文件了。
import ImagePosition from 'image-position'
console.log(ImagePosition)
// {label: '首頁',value: 'home',data: [{label: '輪播', value: 'carousel'}]}
補充vue-cli2.0下如何配置
// build/webpack.base.conf.js
module.exports = {
externals: {
// 新增
'image-position': 'ImagePosition'
}
}
// 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>
總結(jié)
在Vue項目的打包體積優(yōu)化中,cdn加速是常用的一種手段,上面其實就是cdn加速的實現(xiàn)內(nèi)容,把第三方庫通過script標簽引入,大大減少打包的vendor.js文件大小。
當我們想把本地文件放到服務(wù)器遠程化時,關(guān)鍵在于實現(xiàn)步驟的第一步,其他的內(nèi)容跟配置cdn加速的過程是一樣的。
以上就是Vue 如何import服務(wù)器上的js配置文件的詳細內(nèi)容,更多關(guān)于Vue import js配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決Vue-cli3沒有vue.config.js文件夾及配置vue項目域名的問題
- 如何配置vue.config.js 處理static文件夾下的靜態(tài)文件
- Vue的Eslint配置文件eslintrc.js說明與規(guī)則介紹
- vue-cli的build的文件夾下沒有dev-server.js文件配置mock數(shù)據(jù)的方法
- vue-cli腳手架build目錄下utils.js工具配置文件詳解
- vue-cli腳手架config目錄下index.js配置文件的方法
- 詳解vue-cli腳手架build目錄中的dev-server.js配置文件
- 詳解vue-cli中的ESlint配置文件eslintrc.js
相關(guān)文章
Vue全局自適應(yīng)大小:使用postcss-pxtorem方式
本文介紹了如何在Vue項目中使用postcss-pxtorem插件實現(xiàn)響應(yīng)式設(shè)計,postcss-pxtorem可以自動將CSS文件中的px單位轉(zhuǎn)換為rem單位,從而實現(xiàn)更好的自適應(yīng)布局,通過配置postcss-pxtorem插件,可以在構(gòu)建時自動完成轉(zhuǎn)換,無需手動修改代碼2025-01-01
el-upload http-request使用 多個文件上傳攜帶其他參數(shù)方式
這篇文章主要介紹了el-upload http-request使用 多個文件上傳攜帶其他參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
淺談vue項目用到的mock數(shù)據(jù)接口的兩種方式
這篇文章主要介紹了淺談vue項目用到的mock數(shù)據(jù)接口的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10

