gpt4 book ai didi

webpack - npm run build 后 vue-router 不工作

转载 作者:行者123 更新时间:2023-12-02 01:05:12 25 4
gpt4 key购买 nike

我有一个 vue 项目,我已经使用 vue cli 安装了它。

我已经安装了 vue-router 并且所有路由在 npm run dev 模式下都可以正常工作。

URL 是 localhost:8000 。之后,我运行 npm run build 命令,并导航到 url localhost/projet_name 这个正在部署的空白页面并且没有路由工作。

我正在使用 Windows 系统并在 wampp 服务器上工作。

如果我想使用 localhost/projet_name 运行项目我该怎么做?

这是我运行命令 npm run build 后的文件夹结构。

/image/HeoTG.png
webpack.config.js 文件是:

var path = require('path')
var webpack = require('webpack')

module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
}, {
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
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
})
])
}

Packages.json 文件:
{
"name": "posthere",
"description": "A Vue.js project",
"version": "1.0.0",
"author": "Sandip",
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"vue": "^2.5.11",
"vue-resource": "^1.3.5",
"vue-router": "^3.0.1"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}

最佳答案

您必须配置您的 Web 服务器以将所有请求重定向到您的 index.html 文件。

例如,您必须像这样为 Apache 服务器配置 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

您还可以看到 other server configuration examples here :

关于webpack - npm run build 后 vue-router 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com