vue-router在vue2/3区别

构建选项区别

vue2-router

javascript 复制代码
 const router-new VueRouter({
      mode:history,
      base:_name,
    })

vue-next-router

javascript 复制代码
import { createRouter,createWebHistory} from vue-next-router
const router=createRouter({
  history:createHistory(/)
})

在上述代码中我们发现,vue2中的构建选项mode和base,分别为mode和base,而在vue-next-router中变成了,history和history中的第一个参数/.

捕获路由加正则表达式/:catchAll(.*)

vue2-router

javascript 复制代码
 const router=new VueRouter({
  mode:history,
  routes:[
    {path:/user/:a*}
  ]
})

vue-next-router

javascript 复制代码
const router=createRouter({
  history:createWebHistrory(),
  routes:[
    {path:/user/:a:catchAll(.*).component:component}
  ]
})

当路由为 /user/a/b 时,捕获到的 params 为 {"a": "a", "catchAll": "/b"}。

match改变(vue2/3)

match详解点击这里

router.match和router.resolve在vue3中已经被合并,只是签名略有不同。

删除router.getMatchedComponents,可以从router.currentRoute.value.matched 中获取。

router.getMatchedComponents 返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类,不是实例)。通常在服务端渲染的数据预加载时使用。

push 或者 resolve 一个不存在的命名路由时,将会引发错误,而不是导航到根路由 "/" 并且不显示任何内容。

在vue2中会自动跳转到跟路由/中,而vue3会报错。

相关推荐
FungLeo2 小时前
成为全栈·React 管理后台篇·总复盘:从“接口能调通”到“后台值得使用”
前端·react.js·前端框架·项目复盘·成为全栈
Csvn3 小时前
并发模式:让渲染学会排队、插队和让路
前端
可乐鸡翅yeah_3 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
Flynt3 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
JavaGuide4 小时前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel4 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
莪_幻尘6 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
风骏时光牛马6 小时前
AI模型综合能力评测:性能、指令遵循与多场景实测对比
前端
Frag0ut6 小时前
Chrome与Chromium内核浏览器在Windows 11上的新特性全景解析
前端·chrome·windows·web安全·chromium·gemini ai·playready drm
hiahiahia1236 小时前
实现完整 Tool Dispatcher
开发语言·前端