vuejs / vue-router

🚦 The official router for Vue 2
http://v3.router.vuejs.org/
MIT License
18.99k stars 5.06k forks source link

Use dynamic routing prompts Uncaught Error: [vue-router] "path" is required in a route configuration #3856

Closed cMing1997 closed 1 year ago

cMing1997 commented 1 year ago

Version

3.6.5

Reproduction link

codesandbox.io

Steps to reproduce

  1. Encapsulates a CommonRouter that handles common things about routing, exporting routes after use
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);

const router: any = new Router({
    scrollBehavior() {
        return {
            x: 0,
            y: 0
        }
    }
});

router.authCode = '';

router.beforeEach((to, from, next) => {
      next();
});

export default router;
  1. The single-page routing file references the CommonRouter, uses addRoutes to add dynamic routing, and prompts the page : vue-router.esm.js:16 [vue-router] router.addRoutes() is deprecated and has been removed in Vue Router 4. Use router.addRoute() instead.
import router from 'lib/CommonRouter';
const LayoutView = () => import("@/components/LayoutView/index.vue");
const WelcomeIndex= () => import("@/pages/welcome/views/home/index.vue");

router.addRoutes([{
    path: '/',
    component: LayoutView,
    children: [{
        path: 'index',
        name: 'welcomeIndex',
        component: WelcomeIndex
    }]
}],);
export default router
  1. So use addRoute, the page directly error, prompt : Uncaught Error: [vue-router] "path" is required in a route configuration

What is expected?

addRoutes and addRoute One of them is available and error-free

What is actually happening?

There is a warning prompt when using addRoutes, and an error is reported directly when using addRoute

posva commented 1 year ago
router.addRoutes([
  {
    path: "/",
    component: LayoutView,
    children: [
      {
        path: "home",
        name: "home",
        component: Home
      }
    ]
  }
]);

or without the array.


Please, next time consider using the Discord server, Discussions (if enabled in this repository), or StackOverflow for questions first. But feel free to come back and open an issue if it turns out to be a bug 🙂