vue-admin-template菜单路由问题

新手上路,请多包涵

image
后端返回的菜单是这样的,不同的角色返回来的菜单不同
我该怎样做路由?
有没有大佬提供一下思路?
新手提问请多多包涵

阅读 2.2k
1 个回答

这个问题的关键点在于如何将扁平的数据转化为树形数据。
这里有一份代码,供你参考。生成树形数据后剩下的工作还得靠你自己了。

const comments = [
    { id: 1, parent_id: null }, 
    { id: 6, parent_id: null }, 
    { id: 2, parent_id: 1 }, 
    { id: 3, parent_id: 1 }, 
    { id: 4, parent_id: 2 }, 
    { id: 5, parent_id: 4 } 
]; 

const nest = (items, id = null, link = "parent_id") => items.filter(item => item[link] === id).map(item => ({ ...item, children: nest(items, item.id) }));
        
const nestedComments = nest(comments);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题