Tit*_*lum 5 github-pages webpack vue.js vuejs2 vue-cli
我有一个 Vue 2.0 网络应用程序,它在我的计算机上运行没有问题,但如果应用程序不在根目录上运行,我似乎无法让它在服务器上运行。
例如:“ www.someserver.com/my-app/”而不是“ www.someserver.com/”。
我使用了webpack-simple 模板,它具有这个基本的 webpack 配置。如何确保应用程序将从文件夹而不是根目录加载文件?
假设您的服务器在您转到您想要的 url 时已经在为您的 html/js 包提供服务......如果您使用的是 vue-router,您还需要在那里设置基本路径。
const router = new VueRouter({
base: "/my-app/",
routes
})
Run Code Online (Sandbox Code Playgroud)
小智 6
存档 vue.config.js
module.exports = {
/* ... */
publicPath: process.env.NODE_ENV === 'production' ? '/my-app/' : '/'
}
Run Code Online (Sandbox Code Playgroud)
存档 router.js
/* ... */
import { publicPath } from '../vue.config'
/* ... */
export default new Router({
mode: 'history',
base: publicPath,
/* ... */
})
Run Code Online (Sandbox Code Playgroud)
在带有 vite 的 VUE 3 中:
里面 vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vue({
reactivityTransform: true
})
],
base: '/myNewFolder/',
})
Run Code Online (Sandbox Code Playgroud)
更多信息: https: //vitejs.dev/config/#base
我想到了。我确实必须编辑publicPath我的条目webpack.config.js,如下所示:
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
})
])
}
Run Code Online (Sandbox Code Playgroud)
注意<REPO_NAME> publicPath该production部分的条目。
接下来,我还必须更新我的链接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>
Run Code Online (Sandbox Code Playgroud)
此配置用于将Vue-cli 2.0Web 应用程序部署到Github Pages。
| 归档时间: |
|
| 查看次数: |
9724 次 |
| 最近记录: |