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

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

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

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

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

// src/image-position.jsexport 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實(shí)現(xiàn)

首先對image-position.js做一點(diǎn)小改造,暴露一個全局對象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)程化時,關(guān)鍵在于實(shí)現(xiàn)步驟的第一步,其他的內(nèi)容跟配置cdn加速的過程是一樣的。

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

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 蓬安县| 鄄城县| 千阳县| 宁南县| 福鼎市| 绥棱县| 泸溪县| 紫阳县| 宁明县| 健康| 崇仁县| 武宁县| 涪陵区| 台湾省| 哈巴河县| 额尔古纳市| 莆田市| 蕲春县| 阿尔山市| 莱芜市| 博客| 象州县| 华容县| 吉首市| 鹿邑县| 康马县| 泗阳县| 介休市| 云梦县| 建平县| 灵川县| 杭锦旗| 綦江县| 南开区| 桃江县| 探索| 含山县| 龙口市| 浮梁县| 新竹市| 鸡西市|