vue 生成动态路由

2019-08-14  本文已影响0人  请叫我彭彭
let temp = {}, //临时路由
                tree = {}, //左侧菜单
                routers = [] //路由
            //组装路由菜单
            for (let i in arr) {
                let path = arr[i].code
                //组装路由
                let tempRouter = {
                    path: path ? `/${path}` : "",
                    name: "",
                    meta: {
                        id: "" + arr[i].id,
                        parentId: arr[i].parentId,
                        title: arr[i].text,
                        iconCls: arr[i].iconCls,
                        pageParams: arr[i].pageParams
                    },
                    children: []
                }
                if (path) {
                    let name = path.slice(path.lastIndexOf("/"))
                    name = name.split('/')
                    tempRouter.name = name[1]
                    //获取组件路径
                    tempRouter.component = require(`@/views/${path}.vue`).default
                    this.$router.options.routes[1].children.push(tempRouter)
                    routers.push(tempRouter)
                }
                temp[arr[i].id] = tempRouter
            }

            //将子路由也添加到路由
            this.$router.addRoutes(this.$router.options.routes)
上一篇下一篇

猜你喜欢

热点阅读