進入項目所在目錄運行css
npm run build
進入~\config\index.js
在build
下的assetsPublicPath
默認狀況下是'/'
,此時打包的index.html文件中的資源文件(js、css、img)默認狀況都是以/
開頭的絕對路徑,指向http服務器的根路徑
若是想修改成相對路徑則須要將assetsPublicPath
的值修改成'./'
,這樣就是指向index.html的相對路徑了html
將打包生成好的項目部署到服務器,可是訪問SPA項目的前端路由會出現
404
,這是因爲HTTP服務器默認狀況下訪問的是對應目錄下的index.html,此時須要對HTTP服務器作下路由映射,將前端路由地址映射到index.html。前端
如下是SPA項目經常使用的幾種部署方式:
例如前端路由地址:http://localhost/live/292/wonderfulvue
若是隻使用Apache作HTTP服務器,能夠設置Apache的url重定向,將全部的請求路由到index.htmlnode
打開~\Apache\conf\httpd.conf
文件webpack
去除httpd.conf文件中LoadModule rewrite_module modules/mod_rewrite.so
前面的#
號nginx
在httpd.conf文件中添加劇定向規則web
RewriteEngine on # 當訪問路由地址爲 /live 開頭的,則將路由重定向到 /index.html RewriteRule \/live.*$ /index.html
使用nginx作反向代理服務器,配置文件參考:vue-router
server { listen 80; server_name localhost:80; index index.html; root /wwwroot/; location / { try_files $uri $uri/ /index.html; } }
使用node.js作反向代理服務器,配置文件參考:npm
var config = require("./webpack.config.js"); var webpack = require("webpack") var webpackDevServer=require("webpack-dev-server") config.entry.unshift("webpack-dev-server/client?http://localhost:80", "webpack/hot/dev-server"); var compiler = webpack(config); var server = new webpackDevServer(compiler, { contentBase: "build", hot: true, inline: true, historyApiFallback: true, proxy: { '/*': { target: 'loaclhost:8080/', secure: false }, } }); server.listen(80);