Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

路由只有一个children时,当作根路由显示的问题 #2943

Open
liudid opened this issue Jan 15, 2020 · 2 comments
Open

路由只有一个children时,当作根路由显示的问题 #2943

liudid opened this issue Jan 15, 2020 · 2 comments

Comments

@liudid
Copy link

liudid commented Jan 15, 2020

当我的路由下面只有一个children(table)时,这个children(table)被当作根路由显示是没有问题的。
1
2

下面出现的问题:/example路由下面只有一个table的子路由,然后我想把table当作根路由在菜单栏显示,但是我在table这个路由中,还想在添加2个子路由,并设置为hidden:true,就出现了table没有被当作根路由显示的问题。

我的预期结果:table路由做为根路由显示在侧边栏,并且table下的2个设置为hidden:true的不在菜单中显示。
但实际结果是这样的:
100
5

官网的解释:
企业微信截图_15790556923170

请问如何解决此问题???感谢

@yunfeizhu
Copy link

yunfeizhu commented Jan 15, 2020

官网的解释:avatar

官网的意思是指当前根目录下的children(hidden: false)大于1时,会展示根目录。你的路由结构为Example->Table->xxx,根目录为Example,根目录下的children有且仅有一个为Table且hidden:false。看起来的确符合官网的描述,但是细看代码src/layout/components/Sidebar/SidebarItem.vue中

image

只有满足上述条件才会直接渲染el-menu-item,也就是直接将Table当成根目录渲染。我们再来看一下hasOneShowingChild()这个方法做了什么

image

首先筛选出hidden不为false的子路由,再用onlyOneChild保存最后一个hidden不为false的子路由信息。然后判断筛选出的子路由数组的长度是否等于1,等于1则表示当前的跟路由下确实存在只有一个children;等于0,即没有hidden不为false子路由,此时包装一下onlyOneChild为{ ...parent, path: '', noShowingChildren: true }。

注意看v-if条件hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow。根据你的路由形式,显然满足第一个判断条件hasOneShowingChild(item.children, item)为true。但是!onlyOneChild.children这个条件无法满足,因为你Table路由下还有两个children,onlyOneChild就存放的Table的路由

image

如果你想要按照你的想法,只需要在v-if上加上一个判断条件即可。判断onlyOneChild的children的hidden是否为true,若都为true,则通过v-if的判断条件,从而渲染出el-menu-item,得到你想要的结果

代码如下,如有错误,请指正:
image
image

@PanJiaChen
Copy link
Owner

你这个其实有些伪需求,你这种情况 一级目录example存在的意义是什么?为什么不直接table?
要做也能做,需要递归判断子children有没有,但我觉得一般场景不需搞得这么复杂。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants