LoginSignup
0
2

More than 5 years have passed since last update.

vue-routerのHistory モード+webpack-dev-serverでサブディレクトリにアクセスする方法

Last updated at Posted at 2019-01-08

久しぶりにQiitaに帰ってキタ(Qiitaaa)ーーー!!!
どうも、岩元です。

webpack-dev-serverの開発環境の設定でハマったのでメモ。
下記のようにhistoryモードで、children オプションを利用してネストした構成にしたとき、/usersにはアクセスできるが/users/fugaにアクセスしようとすると404errorが返ってくる現象が発生した。

router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

export default new VueRouter({
  mode: "history",
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import("@/pages/Home")
    },
    {
      path: '/users',
      component: () => import("@/pages/Users"),
      children: [
        {
          path: '',
          component: () => import("@/pages/users/Index")
        },
        {
          path: 'fuga',
          component: () => import("@/pages/users/fuga")
        },
}

publicPath が設定されていなかったのが原因だった。

webpack.conf.js
module.exports = {
  mode: 'development',
  devtool: 'source-map',
  entry: {
    app: './src/app.coffee'
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: '[name]-[hash].bundle.js',
+    publicPath: '/'
  },
...

  devServer: {
    open: true,
    writeToDisk: true,
    contentBase: path.join(__dirname, '/'),
    watchContentBase: true,
    port: 8080,
    historyApiFallback: {
      historyApiFallback: true
    }
  },

参考: WebpackDevServer breaks on complex nested routes #113

余談: 最近BiSHにハマっている

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2