Vue 如何import服務(wù)器上的js配置文件
項(xiàng)目中有一個(gè)本地配置文件:
// src/image-position.jsexport 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.jsmodule.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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)3. CSS hack用法案例詳解4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)8. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明10. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
