Vue Auto Routing

1.0.1 · active · verified Sun Apr 19

vue-auto-routing is a JavaScript library designed for automatically generating Vue Router routes by convention, mimicking the file-system-based routing approach popularized by frameworks like Nuxt.js. It leverages `vue-route-generator` under the hood to transform component files within a specified directory (e.g., `src/pages`) into a Vue Router configuration. The library ships with a Webpack plugin that integrates into the build process to dynamically generate these routes. As of its current stable version `1.0.1`, it supports both Vue 2 and Vue 3 projects, with `v1.0.0` introducing official Vue 3 compatibility. While `vue-auto-routing` provides the core generation logic, users often prefer `vue-cli-plugin-auto-routing` for a more integrated setup within Vue CLI projects, which includes additional features like automatic layout resolution. The library has a moderate release cadence, focusing on compatibility and core feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `vue-auto-routing` by configuring its Webpack plugin and consuming the generated routes in a Vue 3 application's router setup. Includes a minimal Webpack configuration example.

import { createApp } from 'vue';
import { createRouter, createWebHistory } from 'vue-router';
import App from './App.vue';

// This 'routes' import is a virtual module provided by vue-auto-routing's Webpack plugin.
// It dynamically contains the generated route configurations based on your 'pages' directory.
import routes from 'vue-auto-routing';

// 1. Configure Vue Router with the automatically generated routes (for Vue 3)
const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 2. Mount your Vue 3 application
createApp(App).use(router).mount('#app');

// --- webpack.config.js (or vue.config.js if using Vue CLI) ---
const VueAutoRoutingPlugin = require('vue-auto-routing/lib/webpack-plugin');
const path = require('path');

module.exports = {
  // ... other webpack configurations ...
  plugins: [
    new VueAutoRoutingPlugin({
      pages: path.resolve(__dirname, 'src/pages'), // REQUIRED: Path to your page components directory
      importPrefix: '@/pages/', // OPTIONAL: Prefix for importing components in generated routes (e.g., '@/pages/Home.vue')
      // output: 'src/router/generated-routes.js', // OPTIONAL: Specify a physical output file for inspection or explicit import
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'), // Ensure '@' alias is configured if used in importPrefix
    },
  },
};

/* Example: src/pages/About.vue
<template>
  <div>About Page</div>
</template>

<route>
{
  "meta": {
    "title": "About Us"
  }
}
</route>
*/

view raw JSON →