vue-router-skilld by harlan-zw/vue-ecosystem-skills
npx skills add https://github.com/harlan-zw/vue-ecosystem-skills --skill vue-router-skilldvue-router版本: 5.0.4 依赖: @babel/generator@^7.28.6, @vue-macros/common@^3.1.1, @vue/devtools-api@^8.0.6, ast-walker-scope@^0.8.3, chokidar@^5.0.0, json5@^2.2.3, local-pkg@^1.1.2, magic-string@^0.30.21, mlly@^1.8.0, muggle-string@^0.4.1, pathe@^2.0.3, picomatch@^4.0.3, scule@^1.3.0, tinyglobby@^0.2.15, unplugin@^3.0.0, unplugin-utils@^0.3.1, yaml@^2.8.2 标签: next: 4.0.13, legacy: 3.6.5, edge: 4.4.0-alpha.3, beta: 5.0.0-beta.2, latest: 5.0.4
参考: 文档 — API 参考、指南 • GitHub Issues — 错误、变通方案、边界情况 • GitHub Discussions — 问答、模式、配方 • 发布说明 — 更新日志、破坏性变更、新 API
本节记录了特定版本的 API 变更 — 请优先关注近期的主要/次要版本发布。
新增: vue-router/vite — v5 将 Vite 插件(原 unplugin-vue-router/vite)直接包含在核心包中;请从 导入
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
vue-router/vite新增: vue-router/auto-routes — v5 导出的自动生成的文件路由列表;之前需要单独的 unplugin-vue-router 包 来源
新增: vue-router/unplugin — v5 导出的用于 Webpack/Rollup/esbuild 插件和工具(VueRouterAutoImports、EditableTreeNode、createRoutesContext 等);之前从 unplugin-vue-router 导入 来源
新增: DataLoaderPlugin + defineBasicLoader (实验性) — v5 将数据加载器直接添加到 vue-router/experimental;之前在 unplugin-vue-router/data-loaders 中。在路由器之前使用 app.use(DataLoaderPlugin, { router }) 安装 DataLoaderPlugin 来源
新增: defineColadaLoader (实验性) — 基于 Pinia Colada 的加载器,位于 vue-router/experimental/pinia-colada;之前是 unplugin-vue-router/data-loaders/pinia-colada 来源
新增: NavigationResult (实验性) — 来自 vue-router/experimental 的类,在加载器内部返回,用于在导航期间重定向(例如 return new NavigationResult('/login'));之前 vue-router 中不存在 来源
新增: Volar 插件已移至 vue-router/volar/sfc-typed-router 和 vue-router/volar/sfc-route-blocks — 之前是 unplugin-vue-router/volar/sfc-typed-router 和 unplugin-vue-router/volar/sfc-route-blocks 来源
新增: TypesConfig 模块增强 — v4.4+ 中用于注册 RouteNamedMap 以实现类型化路由的接口;通过 declare module 'vue-router' { interface TypesConfig { RouteNamedMap: RouteNamedMap } } 进行增强 来源
破坏性变更: IIFE 构建不再捆绑 @vue/devtools-api — v5 将 devtools-api 升级到 v8,该版本没有 IIFE 构建;影响依赖捆绑 devtools 的 CDN/脚本标签设置 来源
新增: 查询参数默认可选(实验性) — v5 文件路由在类型化路由中默认使查询参数变为可选 来源
其他变更: unplugin-vue-router 类型/工具已移至 vue-router/unplugin(重命名)· route-map.d.ts 替换 typed-router.d.ts(重命名)· 路由记录上的 meta.loaders 数组用于手动连接数据加载器(新增,实验性)· router.currentRoute 是 Ref<RouteLocationNormalizedLoaded> — 通过 .value 访问(v4 破坏性变更)· router.onReady() 已移除 — 使用返回 Promise 的 router.isReady()(v4 破坏性变更)· scrollBehavior 的 x/y 重命名为 left/top(v4 破坏性变更)
在守卫中直接使用 route.meta,而不是遍历 to.matched — Vue Router 非递归地合并所有祖先的 meta 字段,因此 to.meta.requiresAuth 已经反映了来自父路由的继承值 来源
通过模块增强扩展 RouteMeta 接口以类型化所有 meta 字段 — 这强制要求每个路由在编译时声明必需的字段,而不是依赖运行时检查 来源
使用 router.beforeResolve(而非 beforeEach)来处理必须在异步组件解析后运行的操作 — 它在所有组件内守卫和异步路由组件准备就绪后触发,使其成为检查摄像头权限或最终数据验证的正确位置 来源
在导航守卫(全局或每路由)内部使用 inject() 来访问 Pinia 存储和提供的值 — 自 Vue 3.3 起支持,避免了在 setup 上下文之外导入存储 来源
避免在守卫中使用 next 回调 — 改为返回值(false、路由位置或不返回任何值);next 容易出错,因为每个代码路径必须恰好调用一次,并且被视为遗留 API 来源
使用 await router.push() 并检查解析值以检测导航失败 — 当被阻止时,Promise 解析为 NavigationFailure,成功时解析为 undefined;使用 isNavigationFailure(result, NavigationFailureType.aborted) 来区分特定的失败类型 来源
在路由记录上设置 props: true(或一个函数)以使组件与 useRoute() 解耦 — 接收参数作为 props 的组件无需路由器实例即可重用和测试;使用函数形式(props: route => ({ query: route.query.q }))来映射查询参数或转换类型 来源
监视特定的 route 属性,而不是整个 route 对象 — useRoute() 返回一个响应式对象,但完全监视它会在任何更改(哈希、查询、参数)时触发;将监视器限制为 () => route.params.id 以避免不必要的获取操作 来源
(实验性)使用从页面组件导出的 defineBasicLoader / defineColadaLoader 进行导航感知的数据获取 — 从懒加载的路由组件导出的加载器会自动连接到导航生命周期,在解析之前阻止转换,并反应式地暴露 isLoading/error;对于不应阻止导航的非关键数据,设置 lazy: true 来源
每周安装量
94
代码仓库
GitHub 星标
147
首次出现
2026年2月19日
安全审计
已安装于
github-copilot65
claude-code62
opencode62
codex62
kimi-cli60
amp60
vue-routerVersion: 5.0.4 Deps: @babel/generator@^7.28.6, @vue-macros/common@^3.1.1, @vue/devtools-api@^8.0.6, ast-walker-scope@^0.8.3, chokidar@^5.0.0, json5@^2.2.3, local-pkg@^1.1.2, magic-string@^0.30.21, mlly@^1.8.0, muggle-string@^0.4.1, pathe@^2.0.3, picomatch@^4.0.3, scule@^1.3.0, tinyglobby@^0.2.15, unplugin@^3.0.0, unplugin-utils@^0.3.1, yaml@^2.8.2 Tags: next: 4.0.13, legacy: 3.6.5, edge: 4.4.0-alpha.3, beta: 5.0.0-beta.2, latest: 5.0.4
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • GitHub Discussions — Q&A, patterns, recipes • Releases — changelog, breaking changes, new APIs
This section documents version-specific API changes — prioritize recent major/minor releases.
NEW: vue-router/vite — v5 ships the Vite plugin (formerly unplugin-vue-router/vite) directly in the core package; import from vue-router/vite instead source
NEW: vue-router/auto-routes — v5 export that provides the auto-generated file-based route list; previously required unplugin-vue-router as a separate package source
NEW: vue-router/unplugin — v5 export for Webpack/Rollup/esbuild plugins and utilities (VueRouterAutoImports, EditableTreeNode, createRoutesContext, etc.); previously imported from unplugin-vue-router source
NEW: DataLoaderPlugin + defineBasicLoader (experimental) — v5 adds data loaders directly to vue-router/experimental; previously in unplugin-vue-router/data-loaders. Install DataLoaderPlugin before the router with app.use(DataLoaderPlugin, { router }) source
NEW: defineColadaLoader (experimental) — Pinia Colada-backed loader available at vue-router/experimental/pinia-colada; previously unplugin-vue-router/data-loaders/pinia-colada source
NEW: NavigationResult (experimental) — class from vue-router/experimental returned inside a loader to redirect during navigation (e.g. return new NavigationResult('/login')); previously did not exist in vue-router source
NEW: Volar plugins moved to vue-router/volar/sfc-typed-router and vue-router/volar/sfc-route-blocks — previously unplugin-vue-router/volar/sfc-typed-router and unplugin-vue-router/volar/sfc-route-blocks source
NEW: TypesConfig module augmentation — v4.4+ interface used to register RouteNamedMap for typed routes; augment with declare module 'vue-router' { interface TypesConfig { RouteNamedMap: RouteNamedMap } } source
BREAKING: IIFE build no longer bundles @vue/devtools-api — v5 upgraded devtools-api to v8 which has no IIFE build; affects CDN/script-tag setups that relied on the bundled devtools source
NEW: Query params optional by default (experimental) — v5 file-based routing makes query params optional in typed routes by default source
Also changed: unplugin-vue-router types/utilities moved to vue-router/unplugin (renamed) · route-map.d.ts replaces typed-router.d.ts (renamed) · meta.loaders array on route records for manually connecting data loaders (NEW, experimental) · router.currentRoute is Ref<RouteLocationNormalizedLoaded> — access via .value (v4 BREAKING) · router.onReady() removed — use router.isReady() returning a Promise (v4 BREAKING) · / renamed to / (v4 BREAKING)
Use route.meta directly in guards instead of iterating to.matched — Vue Router merges all ancestor meta fields non-recursively, so to.meta.requiresAuth already reflects inherited values from parent routes source
Extend the RouteMeta interface via module augmentation to type all meta fields — this enforces that every route declares required fields at compile time rather than relying on runtime checks source
Use router.beforeResolve (not beforeEach) for operations that must run after async components are resolved — it fires after all in-component guards and async route components are ready, making it the correct place for camera permission checks or final data validation
Weekly Installs
94
Repository
GitHub Stars
147
First Seen
Feb 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot65
claude-code62
opencode62
codex62
kimi-cli60
amp60
Genkit JS 开发指南:AI 应用构建、错误排查与最佳实践
7,700 周安装
scrollBehaviorxylefttopUse inject() inside navigation guards (global or per-route) to access Pinia stores and provided values — this is supported since Vue 3.3 and avoids importing stores outside of setup context source
Avoid the next callback in guards — return values (false, a route location, or nothing) instead; next is error-prone because it must be called exactly once per code path and is considered a legacy API source
await router.push() and check the resolved value to detect navigation failures — the promise resolves to a NavigationFailure when blocked, or undefined on success; use isNavigationFailure(result, NavigationFailureType.aborted) to distinguish the specific failure type source
Set props: true (or a function) on route records to decouple components from useRoute() — components receiving params as props are reusable and testable without a router instance; use the function form (props: route => ({ query: route.query.q })) to map query params or cast types source
Watch specific route properties rather than the whole route object — useRoute() returns a reactive object, but watching it entirely triggers on any change (hash, query, params); narrow the watcher to () => route.params.id to avoid unnecessary fetches source
(experimental) Use defineBasicLoader / defineColadaLoader exported from page components for navigation-aware data fetching — loaders exported from lazy-loaded route components are automatically connected to the navigation lifecycle, block the transition until resolved, and expose isLoading/error reactively; set lazy: true for non-critical data that should not block navigation source