【问题标题】:vue-router not working after npm run buildnpm run build 后 vue-router 不工作
【发布时间】:2018-06-14 06:10:39
【问题描述】:

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

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

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

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

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

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

https://i.stack.imgur.com/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"
  }
}

【问题讨论】:

  • 我可以就这个问题寻求帮助吗?
  • 能否请您发布 webpack.config.js 和 packages.json
  • 构建配置中的 dist 目录路径似乎有问题
  • 我尝试过一次 publicPath: 'dist/' (已修改),然后尝试再次运行命令“npm run build”,但没有给我结果。 @TalhaJunaid

标签: webpack vuejs2


【解决方案1】:

您必须配置您的网络服务器以将所有请求重定向到您的 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:

【讨论】:

  • 您好,请避免仅使用代码回答。添加一些关于您的解决方案的说明。
【解决方案2】:

模板中存在此问题。 github 上有一个拉取请求待处理。问题是在 webpack.config.jsindex.html 中提到的 build.js 路径。需要修改webpack.config.js

来自

output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },

output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: './dist/',
    filename: 'build.js'
  },

index.html 中将&lt;script src="/dist/build.js"&gt;&lt;/script&gt; 更改为&lt;script src="./dist/build.js"&gt;&lt;/script&gt;

【讨论】:

  • 根据您的建议,我已更改所有文件,然后运行“ npm run build ”,但结果相同@Talha Junaid
  • 你能分享 index.html 寻找 build.js 文件的路径吗?您可以在“网络”选项卡中找到一个已提交的网络请求,错误为 404
猜你喜欢
  • 2021-02-01
  • 2021-03-11
  • 2021-09-07
  • 2019-10-10
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多