Vue 如何import服務(wù)器上的js配置文件
背景
項(xiàng)目中有一個(gè)本地配置文件:
// src/image-position.js
export default {
label: '首頁(yè)',
value: 'home',
data: [
{
label: '輪播',
value: 'carousel'
}
]
}
如何引用一個(gè)本地文件大家都知道:
import ImagePosition from './image-position.js'
現(xiàn)在需要把image-position.js文件丟到服務(wù)器上去,得到它的鏈接:
xxx.com/static/imag…
這個(gè)時(shí)候你直接引用文件地址自然是行不通的。
import ImagePosition from 'https://xxx.com/static/image-position.js' // ERROR This dependency was not found
實(shí)現(xiàn)
首先對(duì)image-position.js做一點(diǎn)小改造,暴露一個(gè)全局對(duì)象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: '首頁(yè)',
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: '首頁(yè)',value: 'home',data: [{label: '輪播', value: 'carousel'}]}
補(bǔ)充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項(xiàng)目的打包體積優(yōu)化中,cdn加速是常用的一種手段,上面其實(shí)就是cdn加速的實(shí)現(xiàn)內(nèi)容,把第三方庫(kù)通過(guò)script標(biāo)簽引入,大大減少打包的vendor.js文件大小。
當(dāng)我們想把本地文件放到服務(wù)器遠(yuǎn)程化時(shí),關(guān)鍵在于實(shí)現(xiàn)步驟的第一步,其他的內(nèi)容跟配置cdn加速的過(guò)程是一樣的。
以上就是Vue 如何import服務(wù)器上的js配置文件的詳細(xì)內(nèi)容,更多關(guān)于Vue import js配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決Vue-cli3沒(méi)有vue.config.js文件夾及配置vue項(xiàng)目域名的問(wèn)題
- 如何配置vue.config.js 處理static文件夾下的靜態(tài)文件
- Vue的Eslint配置文件eslintrc.js說(shuō)明與規(guī)則介紹
- vue-cli的build的文件夾下沒(méi)有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項(xiàng)目中使用postcss-pxtorem插件實(shí)現(xiàn)響應(yīng)式設(shè)計(jì),postcss-pxtorem可以自動(dòng)將CSS文件中的px單位轉(zhuǎn)換為rem單位,從而實(shí)現(xiàn)更好的自適應(yīng)布局,通過(guò)配置postcss-pxtorem插件,可以在構(gòu)建時(shí)自動(dòng)完成轉(zhuǎn)換,無(wú)需手動(dòng)修改代碼2025-01-01
el-upload http-request使用 多個(gè)文件上傳攜帶其他參數(shù)方式
這篇文章主要介紹了el-upload http-request使用 多個(gè)文件上傳攜帶其他參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue后臺(tái)中優(yōu)雅書(shū)寫(xiě)狀態(tài)標(biāo)簽的方法實(shí)例
在Vue中,我們可以非常便捷地通過(guò)標(biāo)簽實(shí)現(xiàn)狀態(tài)的保存,這篇文章主要給大家介紹了關(guān)于Vue后臺(tái)中如何優(yōu)雅的書(shū)寫(xiě)狀態(tài)標(biāo)簽的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
vuejs手把手教你寫(xiě)一個(gè)完整的購(gòu)物車(chē)實(shí)例代碼
這篇文章主要介紹了vuejs-手把手教你寫(xiě)一個(gè)完整的購(gòu)物車(chē)實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解
今天小編就為大家分享一篇關(guān)于在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
淺談vue項(xiàng)目用到的mock數(shù)據(jù)接口的兩種方式
這篇文章主要介紹了淺談vue項(xiàng)目用到的mock數(shù)據(jù)接口的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

