vue2 与 vue3 的 vue-router的差异

一.引用方式变化 ,routes属性加入强制判断

vue2 的引用方式:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [ ...xxx ]

const router = new Router({
	mode: 'history',
	routes
})

export default router

vue3的引用方式:

 import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

对比可以看出,vue3中
a. router的实例 是通过函数 createRouter 来创建
b. 历史模式的选择 属性 history: xxxxx

并且在vue3中会 强制判断routes,若不存在则编译都不会通过

const router = createRouter({
  history: createWebHashHistory(),
  // routes   // 注释掉routes之后 编译会报错
})

二. 情形区别:在routes不做 notFound 处理 且 浏览器访问路径 不匹配 routes

vue2: 进入根路径
vue3:
在这里插入图片描述
如上图展示,vue3中当浏览器访问路径 不匹配 routes时, 不会跳转到根路径,而是匹配空

三. 动态路由匹配针对(*) 需要使用pathMatch属性

path: * // vue2 写法 但是在vue3中会失效并抛出错误 如下
Error: Catch all routes ("*") must now be defined using a param with a custom regexp

vue2与vue3的配置:

const routes = [
{
    // path: '*',  // vue2  使用*
    path: '/:pathMatch(.*)',  // vue3 使用带有自定义regexp的参数定义
    name: 'notFound',
    component: () => import(/* webpackChunkName: "notFound" */ '../views/NotFound.vue')
  }
]

四. 不再给路径默认添加/,redirect重定向需要写全路径

请添加图片描述

五.命名路由不匹配跳转

vue3:

import router from '@/router'
...
  mounted() {
  // vue3特性 -> 不匹配的命名路由 既不会推送到not-found -> 也不会推送到 / 
    router.push({name: 'errorName'})
  }

throw error:请添加图片描述
这里可以使用

router.push({path: '/not/found'})

成功访问到对应的notfound组件
请添加图片描述

六.router-link 标签化

vue2 使用 tag
vue3 不能使用tag 否则报错 使用slot

 // 这里的custom v-slot="{ navigate }" @click="navigate" 都是固定写法
 <router-link to="/" custom v-slot="{ navigate }">
      <span @click="navigate">Home</span> 
</router-link> |
<router-link to="/about" custom v-slot="{ navigate }">
     <span @click="navigate">about</span> 
</router-link>

如下图可知 已成功标签化:请添加图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2和Vue3的Vue Router在用法上有一些区别。 在Vue2中,我们可以使用以下代码来引入Vue Router: ```javascript import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const routes = [ // 路由配置 ] const router = new Router({ mode: 'history', routes }) export default router ``` 而在Vue3中,我们可以使用以下代码来引入Vue Router: ```javascript import { createRouter, createWebHashHistory } from 'vue-router' import router from '@/router' // 在mounted钩子函数中跳转到命名路由 mounted() { router.push({ name: 'errorName' }) } // 创建 router 实例 const router = createRouter({ history: createWebHashHistory(), // routes // 注释掉routes之后 编译会报错 }) // 强制判断routes是否存在,若不存在则编译不通过 const router = createRouter({ history: createWebHashHistory(), // routes // 注释掉routes之后 编译会报错 }) ``` 在Vue3中,还引入了新的`createRouter`和`createWebHashHistory`函数来创建路由实例和设置路由模式。 另外,一个重要的区别是在Vue3中,如果浏览器访问的路径不匹配routes中的任何路由配置,并且在routes中没有进行notFound处理,那么不会自动跳转到not-found页面。而在Vue2中,如果路径不匹配routes中的任何路由配置,可以通过配置一个catch-all路由来处理notFound情况。 综上所述,Vue2和Vue3的Vue Router在引入方式上有差异,并且在路由匹配和notFound处理方面也有一些区别。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue2 与 vue3 的 vue-router差异](https://blog.csdn.net/qq_39549013/article/details/126269418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值