vue-router-skilld
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesevuejs/router vue-router
vue-routervuejs/router vue-router
vue-routerVersion: 5.0.2 (Feb 2026)
Deps: @babel/generator@^7.28.6, @vue-macros/common@^3.1.1, @vue/devtools-api@^8.0.0, 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 (Feb 2022), legacy: 3.6.5 (Sep 2022), edge: 4.4.0-alpha.3 (Jun 2024), beta: 5.0.0-beta.2 (Jan 2026), latest: 5.0.2 (Feb 2026)
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • GitHub Discussions — Q&A, patterns, recipes • Releases — changelog, breaking changes, new APIs
版本: 5.0.2(2026年2月)
依赖: @babel/generator@^7.28.6, @vue-macros/common@^3.1.1, @vue/devtools-api@^8.0.0, 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(2022年2月), legacy: 3.6.5(2022年9月), edge: 4.4.0-alpha.3(2024年6月), beta: 5.0.0-beta.2(2026年1月), latest: 5.0.2(2026年2月)
参考文档: 官方文档 — API参考、使用指南 • GitHub Issues — 问题反馈、解决方案、边界情况 • GitHub Discussions — 问答、模式、实践方案 • 版本发布记录 — 更新日志、破坏性变更、新API
API Changes
API变更
This section documents version-specific API changes — prioritize recent major/minor releases.
-
NEW:— v5 ships the Vite plugin (formerly
vue-router/vite) directly in the core package; import fromunplugin-vue-router/viteinstead sourcevue-router/vite -
NEW:— v5 export that provides the auto-generated file-based route list; previously required
vue-router/auto-routesas a separate package sourceunplugin-vue-router -
NEW:— v5 export for Webpack/Rollup/esbuild plugins and utilities (
vue-router/unplugin,VueRouterAutoImports,EditableTreeNode, etc.); previously imported fromcreateRoutesContextsourceunplugin-vue-router -
NEW:+
DataLoaderPlugin(experimental) — v5 adds data loaders directly todefineBasicLoader; previously invue-router/experimental. Installunplugin-vue-router/data-loadersbefore the router withDataLoaderPluginsourceapp.use(DataLoaderPlugin, { router }) -
NEW:(experimental) — Pinia Colada-backed loader available at
defineColadaLoader; previouslyvue-router/experimental/pinia-coladasourceunplugin-vue-router/data-loaders/pinia-colada -
NEW:(experimental) — class from
NavigationResultreturned inside a loader to redirect during navigation (e.g.vue-router/experimental); previously did not exist in vue-router sourcereturn new NavigationResult('/login') -
NEW: Volar plugins moved toand
vue-router/volar/sfc-typed-router— previouslyvue-router/volar/sfc-route-blocksandunplugin-vue-router/volar/sfc-typed-routersourceunplugin-vue-router/volar/sfc-route-blocks -
NEW:module augmentation — v4.4+ interface used to register
TypesConfigfor typed routes; augment withRouteNamedMapsourcedeclare module 'vue-router' { interface TypesConfig { RouteNamedMap: RouteNamedMap } } -
BREAKING: IIFE build no longer bundles— v5 upgraded devtools-api to v8 which has no IIFE build; affects CDN/script-tag setups that relied on the bundled devtools source
@vue/devtools-api -
NEW: Query params optional by default (experimental) — v5 file-based routing makes query params optional in typed routes by default source
Also changed: types/utilities moved to (renamed) · replaces (renamed) · array on route records for manually connecting data loaders (NEW, experimental) · is — access via (v4 BREAKING) · removed — use returning a Promise (v4 BREAKING) · / renamed to / (v4 BREAKING)
unplugin-vue-routervue-router/unpluginroute-map.d.tstyped-router.d.tsmeta.loadersrouter.currentRouteRef<RouteLocationNormalizedLoaded>.valuerouter.onReady()router.isReady()scrollBehaviorxylefttop本节记录各版本的API变更信息 — 优先关注近期的大版本/小版本更新。
-
新增:— v5版本将Vite插件(原
vue-router/vite)直接集成到核心包中;请改为从unplugin-vue-router/vite导入 来源vue-router/vite -
新增:— v5新增的导出项,提供基于文件自动生成的路由列表;此前需要单独安装
vue-router/auto-routes包 来源unplugin-vue-router -
新增:— v5为Webpack/Rollup/esbuild提供的插件及工具函数导出项(包含
vue-router/unplugin、VueRouterAutoImports、EditableTreeNode等);此前需从createRoutesContext导入 来源unplugin-vue-router -
新增:+
DataLoaderPlugin(实验性) — v5在defineBasicLoader中直接添加了数据加载器;此前该功能位于vue-router/experimental。请在注册路由前使用unplugin-vue-router/data-loaders安装app.use(DataLoaderPlugin, { router })来源DataLoaderPlugin -
新增:(实验性) — 基于Pinia Colada的加载器,可从
defineColadaLoader获取;此前位于vue-router/experimental/pinia-colada来源unplugin-vue-router/data-loaders/pinia-colada -
新增:(实验性) — 从
NavigationResult导出的类,可在加载器中返回以在导航过程中实现重定向(例如vue-router/experimental);此前vue-router中无此功能 来源return new NavigationResult('/login') -
新增: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 -
新增:模块扩展 — v4.4+版本新增的接口,用于注册
TypesConfig以实现类型化路由;可通过RouteNamedMap进行扩展 来源declare module 'vue-router' { interface TypesConfig { RouteNamedMap: RouteNamedMap } } -
破坏性变更:IIFE构建版本不再捆绑— v5版本将devtools-api升级至v8,该版本无IIFE构建;这会影响依赖捆绑版devtools的CDN/script标签引入方式 来源
@vue/devtools-api -
新增:查询参数默认可选(实验性) — v5版本的基于文件路由功能默认将类型化路由中的查询参数设为可选 来源
其他变更: 的类型定义/工具函数迁移至(已重命名) · 替代(已重命名) · 路由记录新增数组,用于手动关联数据加载器(新增,实验性) · 类型为 — 需通过访问(v4版本破坏性变更) · 已移除 — 请使用返回Promise的(v4版本破坏性变更) · 中的/参数重命名为/(v4版本破坏性变更)
unplugin-vue-routervue-router/unpluginroute-map.d.tstyped-router.d.tsmeta.loadersrouter.currentRouteRef<RouteLocationNormalizedLoaded>.valuerouter.onReady()router.isReady()scrollBehaviorxylefttopBest Practices
最佳实践
-
Usedirectly in guards instead of iterating
route.meta— Vue Router merges all ancestorto.matchedfields non-recursively, sometaalready reflects inherited values from parent routes sourceto.meta.requiresAuth -
Extend theinterface via module augmentation to type all
RouteMetafields — this enforces that every route declares required fields at compile time rather than relying on runtime checks sourcemeta -
Use(not
router.beforeResolve) 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 sourcebeforeEach -
Useinside 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
inject()context sourcesetup -
Avoid thecallback in guards — return values (
next, a route location, or nothing) instead;falseis error-prone because it must be called exactly once per code path and is considered a legacy API sourcenext -
and check the resolved value to detect navigation failures — the promise resolves to a
await router.push()when blocked, orNavigationFailureon success; useundefinedto distinguish the specific failure type sourceisNavigationFailure(result, NavigationFailureType.aborted) -
Set(or a function) on route records to decouple components from
props: true— components receiving params as props are reusable and testable without a router instance; use the function form (useRoute()) to map query params or cast types sourceprops: route => ({ query: route.query.q }) -
Watch specificproperties rather than the whole
routeobject —routereturns a reactive object, but watching it entirely triggers on any change (hash, query, params); narrow the watcher touseRoute()to avoid unnecessary fetches source() => route.params.id -
(experimental) Use/
defineBasicLoaderexported 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 exposedefineColadaLoader/isLoadingreactively; seterrorfor non-critical data that should not block navigation sourcelazy: true
-
在导航守卫中直接使用,而非遍历
route.meta— Vue Router会以非递归方式合并所有父路由的to.matched字段,因此meta已包含从父路由继承的值 来源to.meta.requiresAuth -
通过模块扩展接口,为所有
RouteMeta字段添加类型定义 — 这能确保在编译阶段强制每个路由声明必填字段,而非依赖运行时检查 来源meta -
使用(而非
router.beforeResolve)执行需在异步组件解析后运行的操作 — 该钩子会在所有组件内守卫和异步路由组件准备完成后触发,是进行相机权限检查或最终数据验证的合适时机 来源beforeEach -
在导航守卫(全局或路由级)中使用访问Pinia状态管理仓库和注入的值 — Vue 3.3及以上版本支持该方式,可避免在
inject()上下文之外导入仓库 来源setup -
避免在守卫中使用回调函数 — 改用返回值(
next、路由地址或无返回值);false容易出错,因为它在每个代码路径中必须被调用恰好一次,且属于遗留API 来源next -
使用并检查解析结果以检测导航失败 — 当导航被阻止时,Promise会解析为
await router.push()对象,成功时则返回NavigationFailure;可使用undefined区分具体的失败类型 来源isNavigationFailure(result, NavigationFailureType.aborted) -
在路由记录中设置(或函数形式),使组件与
props: true解耦 — 通过props接收参数的组件无需路由实例即可复用和测试;使用函数形式(useRoute())可映射查询参数或进行类型转换 来源props: route => ({ query: route.query.q }) -
监听对象的特定属性,而非整个
route对象 —route返回一个响应式对象,但监听整个对象会在任何变化(哈希、查询参数、路径参数)时触发回调;将监听器限定为useRoute()可避免不必要的数据请求 来源() => route.params.id -
(实验性)在页面组件中导出/
defineBasicLoader,实现导航感知的数据获取 — 从懒加载路由组件导出的加载器会自动关联导航生命周期,在解析完成前阻止页面过渡,并以响应式方式暴露defineColadaLoader/isLoading状态;对于非关键数据,可设置error使其不阻塞导航 来源lazy: true