Skip to content

路径最长匹配 ​

本节目标:理解“路径最长匹配”的核心思路,并能把它用于实际开发或面试表达。 /* 根据当前路径,精确匹配顶级菜单:

  1. 只有在 path === '/' 时才匹配 'public'
  2. 其它情况取 “最长前缀” 匹配,避免 /trade 被 '/' 抢先匹配 */ const _selectedKeys = computed({ get() { const path = route.path if (path === '/' || path === '') return ['public'] const match = items.value .filter((i) => i.to !== '/' && path.startsWith(i.to as string)) .sort((a, b) => (b.to as string).length - (a.to as string).length)[0] return [match?.key ?? ''] }, set(v) { emit('update:selectedKeys', v) }, })

本站总访问 — 次