vue路由优化

很多小伙伴写的路由都是直接在路由文件里面编写,这样会导致路由文件经常出现上千行的情况,整个文件会非常地繁琐。
我们可以新建一个路由文件夹
image.png
然后在里面引入

export default{
    path: '/index',
    name: 'index',
    component: ()=> import('../view/index.vue'),
    children: [
        // 子路由
    ]
}

然后在总路由index.js里引入

const routerList = [];
function importAll (route){
    route.keys().forEach(
        (key) => routerList.push(route(key).default)
    );
}

importAll(require.context('./', true, /\.routes\.js/));

const routes = [
    {
        path:
        name:
        conmponent:
    },
    ...routerList,
]

评论

暂无评论