vue 動態獲取路由在對組件進行處理是報錯,導致無法進入頁面
function filterAsyncRouter(asyncRouterMap) { //遍歷后台傳來的路由字符串,轉換為組件對象
const accessedRouters = asyncRouterMap.filter(route => {
if (route.component) { if (route.component === 'Layout') {//Layout組件特殊處理 route.component = Layout } else { console.log('@/view'+ route.component) route.component = () => import('@/view'+ route.component) //這樣不行,報錯信息如下 //route.component = () => import('@/view/company/basicInfo') //這樣是可以進入到這樣頁面的 } } if (route.children && route.children.length) { route.children = filterAsyncRouter(route.children) } return true
})
return accessedRouters
}
報錯信息
解決辦法:
找到問題了,是因為當異步執行的時候,router.component已經成了一個函數,不是一個字符串了,改為下面這種寫法就可以了
const component = route.component
route.component = resolve => require(['@/view'+ component], resolve)