久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Vue 如何import服務(wù)器上的js配置文件

瀏覽:36日期:2022-09-30 18:08:36
背景

項(xiàng)目中有一個(gè)本地配置文件:

// src/image-position.jsexport default { label: ’首頁’, 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: ’首頁’, 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’}]}補(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)容,把第三方庫通過script標(biāo)簽引入,大大減少打包的vendor.js文件大小。

當(dāng)我們想把本地文件放到服務(wù)器遠(yuǎn)程化時(shí),關(guān)鍵在于實(shí)現(xiàn)步驟的第一步,其他的內(nèi)容跟配置cdn加速的過程是一樣的。

以上就是Vue 如何import服務(wù)器上的js配置文件的詳細(xì)內(nèi)容,更多關(guān)于Vue import js配置文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 井冈山市| 黄山市| 汤原县| 玉田县| 兴仁县| 文安县| 虎林市| 铁岭县| 宁强县| 小金县| 武义县| 承德市| 中江县| 肥东县| 武定县| 德兴市| 措勤县| 郯城县| 若羌县| 鸡西市| 云梦县| 安西县| 托克托县| 新乡市| 临西县| 鸡西市| 石楼县| 临颍县| 周口市| 新安县| 兴化市| 鸡西市| 财经| 平泉县| 宜宾县| 赤水市| 龙南县| 富蕴县| 阳新县| 新竹县| 萍乡市|