vue新版router.addRouteの基本的な使い方

4830 ワード

vue新版router.addRouteの基本的な使い方
新しいVue Routerでは、従来のrouter.addRouteの代わりにrouter.addRoutesを使用してルーティング、サブルーティングを動的に追加します.
サブルーティングを追加するとき
例えば、既存のルーティング
const routes = [
    {
     
        path: '/',
        name: 'Login',
        component: () => import(/* webpackChunkName: "about" */ '@/views/Login.vue')
    },
    {
     
        path: '/index',
        name: 'index',
        meta: {
      title: '  ', noCache: true },
        component: () => import(/* webpackChunkName: "about" */ '@/views/index.vue'),
        children:[]
        // children: [{
     
        //     path: '/test',
        //     name: 'test',
        //     component: () => import('../views/test.vue')
        // }
                // ]
    }
]

indexでサブルーティングtestを動的に追加するには、特に追加したサブルーティングのpathは必ず親ルーティングのパスも持参することに注意してください
const routeObj = {
     
    path: 'index/test', //              
    name: 'test',
    meta: {
      title: '    test', noCache: true },
    component: () =>
        import('../test/test.vue'),
}
this.$router.addRoute('index', routeObj)