11
This commit is contained in:
@@ -34,48 +34,166 @@ export default {
|
|||||||
default: true
|
default: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
navItemsCache: null
|
||||||
|
}
|
||||||
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters(['device']),
|
...mapGetters(['device', 'sidebarRouters']),
|
||||||
isMobile() {
|
isMobile() {
|
||||||
return this.device === 'mobile' || window.innerWidth < 768
|
return this.device === 'mobile' || window.innerWidth < 768
|
||||||
},
|
},
|
||||||
navItems() {
|
navItems() {
|
||||||
|
// 如果提供了自定义items,直接使用
|
||||||
if (this.items && this.items.length > 0) {
|
if (this.items && this.items.length > 0) {
|
||||||
return this.items
|
return this.items
|
||||||
}
|
}
|
||||||
// 默认导航项 - 从路由中获取
|
|
||||||
const routes = this.$store?.state?.permission?.routes || []
|
|
||||||
const mainRoutes = routes.filter(route => route.meta && route.meta.title && !route.hidden)
|
|
||||||
|
|
||||||
if (mainRoutes.length > 0) {
|
// 使用缓存,避免重复计算
|
||||||
return mainRoutes.slice(0, 5).map(route => ({
|
if (this.navItemsCache) {
|
||||||
path: route.path,
|
return this.navItemsCache
|
||||||
label: route.meta.title,
|
|
||||||
icon: route.meta.icon || 'el-icon-menu',
|
|
||||||
iconClass: route.meta.icon
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有路由,返回默认导航
|
// 从侧边栏路由中获取可用的路由
|
||||||
return [
|
const routes = this.sidebarRouters || []
|
||||||
|
|
||||||
|
// 扁平化路由,获取所有叶子节点路由
|
||||||
|
const flattenRoutes = (routes, parentPath = '') => {
|
||||||
|
let result = []
|
||||||
|
if (!routes || !Array.isArray(routes)) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
routes.forEach(route => {
|
||||||
|
if (route.hidden) return
|
||||||
|
|
||||||
|
// 处理路径 - 确保路径正确
|
||||||
|
let fullPath = route.path || ''
|
||||||
|
if (parentPath) {
|
||||||
|
if (fullPath.startsWith('/')) {
|
||||||
|
fullPath = fullPath
|
||||||
|
} else {
|
||||||
|
// 合并路径
|
||||||
|
const basePath = parentPath.endsWith('/') ? parentPath.slice(0, -1) : parentPath
|
||||||
|
fullPath = `${basePath}/${fullPath}`.replace(/\/+/g, '/')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保路径以/开头
|
||||||
|
if (fullPath && !fullPath.startsWith('/')) {
|
||||||
|
fullPath = '/' + fullPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有子路由,递归处理
|
||||||
|
if (route.children && route.children.length > 0) {
|
||||||
|
result = result.concat(flattenRoutes(route.children, fullPath))
|
||||||
|
} else {
|
||||||
|
// 叶子节点路由,且有meta信息
|
||||||
|
if (route.meta && route.meta.title && fullPath) {
|
||||||
|
result.push({
|
||||||
|
path: fullPath,
|
||||||
|
label: route.meta.title,
|
||||||
|
icon: route.meta.icon || 'el-icon-menu',
|
||||||
|
iconClass: route.meta.icon,
|
||||||
|
route: route
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
const flatRoutes = flattenRoutes(routes)
|
||||||
|
|
||||||
|
// 过滤并选择前5个主要路由
|
||||||
|
const mainRoutes = flatRoutes
|
||||||
|
.filter(route => {
|
||||||
|
// 过滤掉一些特殊路由
|
||||||
|
const excludePaths = ['/redirect', '/login', '/register', '/404', '/401', '/user/profile']
|
||||||
|
const path = route.path || ''
|
||||||
|
return path &&
|
||||||
|
path !== '/' &&
|
||||||
|
!excludePaths.some(exclude => path.includes(exclude)) &&
|
||||||
|
!path.startsWith('/user/')
|
||||||
|
})
|
||||||
|
.slice(0, 5)
|
||||||
|
|
||||||
|
// 缓存结果
|
||||||
|
if (mainRoutes.length > 0) {
|
||||||
|
this.navItemsCache = mainRoutes
|
||||||
|
return mainRoutes
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有找到路由,返回默认导航
|
||||||
|
const defaultRoutes = [
|
||||||
{
|
{
|
||||||
path: '/index',
|
path: '/sloworder/index',
|
||||||
label: '首页',
|
label: '首页',
|
||||||
icon: 'el-icon-s-home'
|
icon: 'el-icon-s-home'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
this.navItemsCache = defaultRoutes
|
||||||
|
return defaultRoutes
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
sidebarRouters: {
|
||||||
|
handler() {
|
||||||
|
// 路由变化时清除缓存
|
||||||
|
this.navItemsCache = null
|
||||||
|
},
|
||||||
|
deep: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
// 等待路由加载完成
|
||||||
|
this.$nextTick(() => {
|
||||||
|
// 延迟一下,确保路由已经加载
|
||||||
|
setTimeout(() => {
|
||||||
|
this.navItemsCache = null
|
||||||
|
this.$forceUpdate()
|
||||||
|
}, 500)
|
||||||
|
})
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
isActive(path) {
|
isActive(path) {
|
||||||
return this.$route.path.startsWith(path)
|
if (!path) return false
|
||||||
|
const currentPath = this.$route.path
|
||||||
|
return currentPath === path || currentPath.startsWith(path + '/')
|
||||||
},
|
},
|
||||||
handleNavClick(item) {
|
handleNavClick(item) {
|
||||||
if (item.handler) {
|
if (item.handler) {
|
||||||
item.handler()
|
item.handler()
|
||||||
} else if (item.path) {
|
this.$emit('nav-click', item)
|
||||||
this.$router.push(item.path)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (item.path) {
|
||||||
|
// 确保路径正确
|
||||||
|
let path = item.path
|
||||||
|
if (!path.startsWith('/')) {
|
||||||
|
path = `/${path}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除末尾的斜杠(除了根路径)
|
||||||
|
if (path !== '/' && path.endsWith('/')) {
|
||||||
|
path = path.slice(0, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试导航
|
||||||
|
this.$router.push(path).catch(err => {
|
||||||
|
// 如果push失败,尝试replace
|
||||||
|
if (err.name !== 'NavigationDuplicated') {
|
||||||
|
this.$router.replace(path).catch(() => {
|
||||||
|
console.error('Navigation to', path, 'failed')
|
||||||
|
// 不显示错误消息,避免打扰用户
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
this.$emit('nav-click', item)
|
this.$emit('nav-click', item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,10 @@
|
|||||||
<settings ref="settingRef"/>
|
<settings ref="settingRef"/>
|
||||||
</div>
|
</div>
|
||||||
<!-- 移动端底部导航 -->
|
<!-- 移动端底部导航 -->
|
||||||
<mobile-bottom-nav v-if="device === 'mobile'" />
|
<mobile-bottom-nav
|
||||||
|
v-if="device === 'mobile'"
|
||||||
|
:items="mobileNavItems"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -42,6 +45,11 @@ export default {
|
|||||||
needTagsView: state => state.settings.tagsView,
|
needTagsView: state => state.settings.tagsView,
|
||||||
fixedHeader: state => state.settings.fixedHeader
|
fixedHeader: state => state.settings.fixedHeader
|
||||||
}),
|
}),
|
||||||
|
mobileNavItems() {
|
||||||
|
// 可以从配置或store中获取,也可以自定义
|
||||||
|
// 如果返回空数组,组件会使用默认逻辑
|
||||||
|
return []
|
||||||
|
},
|
||||||
classObj() {
|
classObj() {
|
||||||
return {
|
return {
|
||||||
hideSidebar: !this.sidebar.opened,
|
hideSidebar: !this.sidebar.opened,
|
||||||
|
|||||||
Reference in New Issue
Block a user