vue路由的二次封装

路由的二次封装
var routes = [
    {
        path: "/attendance",
        component: () => import('../pages/Attendance')
    },
    {
        path: "/identify",
        component: () => import('../pages/Identify')
    },
    {
        path: "/",
        redirect: "/attendance"
    },
]
// 获取路由信息方法
let getRoutes = function() {
	// 自动生成路由
	createRoute(routes)
	return routes
}
// 自动生成路由
function createRoute(arr) {
	for (let i = 0; i < arr.length; i++) {
		if (!arr[i].component) return
		// 自动生成component
		let componentFun = import(`../views/${arr[i].component}.vue`)
		arr[i].component = () => componentFun
		if (arr[i].children && arr[i].children.length > 0) {
			createRoute(arr[i].children)
		}
	}
}
export default getRoutes()
原文地址:https://www.cnblogs.com/cupid10/p/14247830.html