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会报错。

相关推荐
用户0595401744610 小时前
用了3年Redis,才发现我一直没搞懂缓存一致性测试
前端·css
swipe10 小时前
07|(前端转后全栈)为什么后端也要缓存?从前端缓存思维理解 Redis
前端·后端·全栈
IT小盘10 小时前
05-企业项目统一接入多个大模型-适配器模式实战
前端·人工智能·适配器模式
swipe10 小时前
06|(前端转后全栈)登录后端到底在做什么?JWT、Spring Security 和权限链路
前端·后端·全栈
海上彼尚11 小时前
Nodejs也能写Agent - 22.LangGraph篇 - 上下文工程
前端·javascript·人工智能·langchain·node.js
阳光是sunny11 小时前
LangGraph实战教程:预定义状态MessagesState与AgentState
前端·人工智能·后端
muddjsv12 小时前
CSS选择器体系精讲:按身份、结构、状态精准匹配元素(零基础吃透)
前端·css
程序软件分享12 小时前
多语言DAPP综合盘交易所系统源码搭建剖析
vue.js·金融·开源
GuWenyue12 小时前
等AI回复卡顿到劝退?Vue3+DeepSeek流式输出实战,70行代码实现打字机效果
前端·人工智能·客户端