我有一个Vue 2.0网络应用程序,它在我的计算机上运行时没有问题,但如果没有在根目录上运行的应用程序,我似乎无法让它在服务器上运行。
例如:"www.someserver.com/my-app/
"而不是"www.someserver.com/
"。
我使用了webpack简单模板,它有这个基本的webpack配置。我如何确保应用程序将从文件夹而不是根目录加载文件?
在文件vue.config.js
上
module.exports = {
/* ... */
publicPath: process.env.NODE_ENV === 'production' ? '/my-app/' : '/'
}
文件router.js
/* ... */
import { publicPath } from '../vue.config'
/* ... */
export default new Router({
mode: 'history',
base: publicPath,
/* ... */
})
假设当您转到所需的url时,服务器已经在为您的html/js捆绑包提供服务。。。。如果你使用的是vue路由器,你还需要在那里设置基本路径。
const router = new VueRouter({
base: "/my-app/",
routes
})
我想明白了。我确实不得不在webpack.config.js
中编辑publicPath
条目,就像这样:
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
中的链接,以使用点表示法,而不仅仅是常规的相对路径:
<!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 Pages。
在VUE 3中使用vite:
内部vite.fig.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vue({
reactivityTransform: true
})
],
base: '/myNewFolder/',
})
More info : https://vitejs.dev/config/#base
This has recently been a problem for me. And the above solutions did not work.
The below solution works forvue 2.6
, andvue-router 3.1
What I did was to add a relative path as as suggested in this git issue invue.config.js
:
module.exports = {
publicPath: './',
// Your config here
}
但这并不是整个解决方案,因为没有渲染路由器视图,因此有必要在路由器中添加基本路径。基本路径必须与vue.config.js's publicPath
相同,如本论坛问题中所建议的,要使用location.pathname
。router.js
中的完整解决方案是:
mode: 'history',
base: location.pathname,
routes: [
// Your routes here
]