vue項目打包部署到服務器的方法示例
上上一篇我寫過一些關于vue項目部署到linux服務器的文章,但是那是以node作為開發(fā)環(huán)境 pm2 守護進程的方式,讓他能正常運行,可是還是出現(xiàn)了問題,因為屬于與APP交互的頁面,在webView中打開過慢,APP的用戶體驗非常的差,所以我查找了資料,改變了部署方式,接下來我介紹一下
這一次,我想Tomcat為例
我們先看一下Linux中 Tomcat下面的目錄結構:
以vue-cli 搭建出來的手腳架 webpack的模板下的/config/index.js,這里可以看到assetsPublicPath這個鍵,而且還有兩次,中間我自己挖過的坑我就不說了,這里要說的是,剛才兩個鍵的后面都進行一次修改,都加一個 './'
為什么要改這里呢,是因為路徑問題,如果不修改,部署到Tomcat上會出現(xiàn)空白頁
接下來我來貼出我修改后的config/index.js的配置
'use strict' // Template version: 1.1.3 // see http://vuejs-templates.github.io/webpack for documentation. const path = require('path') module.exports = { build: { env: require('./prod.env'), index: path.resolve(__dirname, '../dist/index.html'), assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: './', productionSourceMap: true, // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report }, dev: { env: require('./dev.env'), port: process.env.PORT || 4000, autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: {}, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false } }
是不是修改的都是 assetsPublicPath這個鍵的值 "/" ,改成"./"
這里我還想提一下我中間遇到的坑:
在開發(fā)模式的時候我們會在這里配置proxyTable: {}, 配置他的原因是為了開發(fā)的時候解決前后端分離跨域問題的
這里一般我們會這么去寫
dev: { env: require('./dev.env'), port: 4000, autoOpenBrowser: true, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api': { changeOrigin: true, target: 'http://192.168.0.116:8080', pathRewrite: { '^/api': '' } } },
記住,這么寫是為了開發(fā)模式的時候方便前后分離開發(fā),但是我們在打包的時候一定要去掉這一部分了,因為在同一環(huán)境同端口下是不存在跨域問題的了
而我這里打包的時候就把這一部分給去掉了
變成proxyTable: {}
與此同時,我們在開發(fā)模式的時候寫axios時會在接口前面加一個"/api" 我們在打包之前同樣要去掉,變成后端給的那種接口,這樣在部署到服務器的時候,接口路徑才能正確
接下來我們還需要修改一個地方 vue-router
vue單頁面應用絕大部分都用到了這個vue-router,所以我們這里也需要做一部分修改就需要給 src/router/index.js添點東西,如下面:
export default new Router({ mode : 'history', base: '/dist/', //添加的地方 routes: [ { path: '/', name: 'index', component: index } ] })
然后我們再執(zhí)行npm run build ,就能發(fā)現(xiàn)我們打包出來的一個文件dist 而這個打包好的文件在這個項目的根目錄下,我們把他放到Tomcat的目錄下的WebApps中,就跨域訪問到你的頁面了
http://59.111.111.11:4000/dist/
備注:記得開通服務器上的端口號,要不然也是訪問失敗。
需要注意的是:圖片資源命名的時候不要有中文,因為中文的話服務器訪問可能圖片顯示不出來。
如果遇到Vue 項目部署到服務器的問題,請點擊此文章http://www.dbjr.com.cn/article/129750.htm,或許能找到解決方法
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用Vue.js實現(xiàn)一個循環(huán)倒計時功能
在Web應用中,倒計時功能常用于各種場景,如活動倒計時、定時任務提醒等,Vue.js作為一款輕量級的前端框架,提供了豐富的工具和API來實現(xiàn)這些功能,本文將詳細介紹如何使用Vue.js實現(xiàn)一個循環(huán)倒計時功能,需要的朋友可以參考下2024-09-09vue給input file綁定函數(shù)獲取當前上傳的對象完美實現(xiàn)方法
這篇文章主要介紹了vue給input file綁定函數(shù)獲取當前上傳的對象完美實現(xiàn)方法,需要的朋友可以參考下2017-12-12