美文网首页
vue 生成动态路由

vue 生成动态路由

作者: 请叫我彭彭 | 来源:发表于2019-08-14 20:44 被阅读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)

相关文章

网友评论

      本文标题:vue 生成动态路由

      本文链接:https://www.haomeiwen.com/subject/sgznjctx.html