router.js路由配置文件
import Router from 'vue-router'
const createRouter = ()=> new Router({
mode: 'history',
routes: []
})
const router = createRouter()
// 在路由上加一个全局方法,用于动态添加路由
router.$addRouterPlus = (params)=>{
router.matcher = new Router({mode: 'history'}).matcher
const rList = [...params]
router.addRoutes(rList)
}
return router
用变量的形式引入组件
require
:可以正常引入
import
:用这个引入我这边是没有生效的,网上查资料有说是路径用全变量形式识别不了,但我加上一段固定路径也不生效(@/views/${path}
)
const path= 'views/projectManage/projectInfo.vue'//文件路径
const menuPathList = [{
path: '/project',
// component: () => import(`@/${path}`)
component: (resolve)=>require([`@/${path}`], resolve)
}]
// 更新路由配置
this.$router.$addRouterPlus(this.menuPathList)