如何在部署Vue应用程序时更改基本路径 [英] How to change basepath on deployment of Vue app
问题描述
我有一个Vue 2.0 Web应用程序,它可以在我的计算机上正常运行,但是如果没有在根目录上运行该应用程序,我似乎无法使其在服务器上正常工作.
I have a Vue 2.0 webapplication that runs without problems on my computer, but I can't seem to get it to work on a server without the app running on the root directory.
例如:"www.someserver.com/my-app/
"而不是"www.someserver.com/
".
e.g: 'www.someserver.com/my-app/
' instead of 'www.someserver.com/
'.
我使用了 webpack-simple模板,该模板具有
I used the webpack-simple template which has this basic webpack configuration. How can I make sure that the app will load the files from the folder instead of the root?
推荐答案
我知道了.我确实必须在我的webpack.config.js
中编辑publicPath
条目,就像这样:
I figured it out. I indeed had to edit the publicPath
entry in my webpack.config.js
, like so:
var path = require('path')
var webpack = require('webpack')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
plugins: [new ExtractTextPlugin("main.css")],
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.output.publicPath = '/<REPO_NAME>/dist/';
module.exports.devtool = '#source-map';
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
/*new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),*/
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
注意production
部分中的<REPO_NAME> publicPath
条目.
接下来,我还必须更新index.html
中的链接以使用点符号而不是常规的相对路径:
Next, I also had to update the links in my index.html
to use the dot-notation instead of just regular relative paths:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>listz-app</title>
<link rel="stylesheet" href="./dist/main.css">
</head>
<body>
<div id="app"></div>
<script src="./dist/build.js"></script>
</body>
</html>
此配置可将Vue-cli 2.0
Web应用程序部署到 Github页面.
This configuration works to deploy Vue-cli 2.0
webapplication to Github Pages.
这篇关于如何在部署Vue应用程序时更改基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!