Vue項目webpack打包部署到服務器

http://blog.csdn.net/Dear_Mr/article/details/72871919?locationNum=5&fps=1javascript

 

Vue項目webpack打包部署到服務器

這篇博文主要說的就是我今天遇到的問題,並且在通過個人詢問,好多人在打包部署的時候都遇到了一些問題,下面就來講下,如何將Vue項目放置在服務器上,這裏以Tomcat爲例。css

必需要配置的就是/config/index.js

在vue-cli webpack的模板下的/config/index.js,咱們能夠看到assetsPublicPath這個鍵,而且這個東西還出現了兩次,我第一次打包的時候,只是修改了最下面的assetsPublicPath,將它從'/'變爲了./,而後我就執行了npm run build,打包成功以後,能夠看到項目中會多出來一個文件夾,就是dist,裏面有一個static文件夾和index.html,而後我就將dist目錄下的文件拷貝到Tomcat服務器下,會發現訪問到的是一個空白頁面,可是當我把它放在..\webapps\ROOT目錄下,它就能夠訪問了html

Tomcat下面的目錄結構:vue

這裏寫圖片描述

可是這確定是不行的,而後我就開始尋找答案,也根據別人的一些步驟作了下來,後來發現仍是有一些問題的,沒有辦法訪問到主頁面,雖然吧,一直都沒成功,可是我也沒放棄,而後就綜合了一下問答裏面別人說的,進行了幾回嘗試,最後成功了。(給你們一個小建議:別放棄就好)。java

下面的就是個人config/index.js的配置:webpack

// see http://vuejs-templates.github.io/webpack for documentation. var 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: 8080, 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所對應的值,我這裏用的是./,我也用webapps下的命的項目名試過,只是沒獲得我想要的結果,後來我仍是改爲了./git

使用vue-router的狀況

當你在項目中使用vue-router的時候,就須要給src/router/index.js添點東西,以下面:github

export default new Router({ mode : 'history', base: '/ttms/', //添加的地方 ... }

 

而後執行npm run dev,將打包後的文件放在Tomcat的目錄下的WebApps下的ttms中,而後,就能夠訪問到了:http://localhost:8080/ttms/web

相關文章
相關標籤/搜索