vue-router @4.x 和 @3.x 对比

1. 创建实例

// vue2.x router
import Vue from 'vue'
import Router from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    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/About.vue')
  }
]

Vue.use(Router)

const router = new Router({
  base: process.env.BASE_URL,
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes
})

export default router

// vue3.x router
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    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/About.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

2. 路由重定向

  • vue2.x使用路由选项redirect设置路由自动调整,vue3.x中移除了这个选项,将在子路由中添加一个空路径路由来匹配跳转
// vue2.x router
[
  {
    path: '/',
    component: Layout,
    name: 'WebHome',
    meta: { title: '平台首页' },
    redirect: '/dashboard', // 这里写跳转
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        meta: { title: '工作台' },
        component: () => import('../views/dashboard/index.vue')
      }
    ]
  }
]

// vue3.x router
[
  {
    path: '/',
    component: Layout,
    name: 'WebHome',
    meta: { title: '平台首页' },
    children: [
      { path: '', redirect: 'dashboard' }, // 这里写跳转
      {
        path: 'dashboard',
        name: 'Dashboard',
        meta: { title: '工作台' },
        component: () => import('../views/dashboard/index.vue')
      }
    ]
  }
]

3. 匹配所有罗 , /:catchAll(.*)

  • 捕获所有路由 ( /* ) 时,现在必须使用带有自定义正则表达式的参数进行定义:/:catchAll(.*)
// vue2.x router
const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/user/:a*' },
  ]
})

// vue3.x router
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/user/:a:catchAll(.*)', component: component },
  ]
})

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

4. 获取路由

  • 不要相信网上的什么通过ctx访问routerstore对象等之类的,但是其实这种方式只能在develement模式有效,在production环境编译后,ctxdevelement下看到的属性都无法访问,容易误导大家,
    应该相信官方文档给我们推荐的方法, 使用useRoute()等钩子函数:
import { getCurrentInstance } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'

export default {
  setup () {
    const { ctx } = getCurrentInstance()
    console.log(ctx)
    const router = useRouter()
    const route = useRoute()
    const store = useStore()
    console.log(router, route, store)
    console.log(router.currentRoute.value)
    const userId = computed(() => store.state.app.userId)
    return {
      userId
    }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介 Vue Router[https://next.router.vuejs.org/] 是 Vue[https...
    Whyn阅读 7,032评论 0 1
  • (一) 基本模块 01基础模板语法 1.1插值语法 -- 解析标签体里的内容 data -> {{}} 里可以写j...
    SteinsGate_5e01阅读 9,138评论 0 31
  • 前言 Vue3.0的步伐越来越近了,是时候了解起来了,虽然嘴上还喊学不动了,但是,身体还得诚实起来,接着学。。。通...
    Mstian阅读 13,329评论 1 9
  • 异步组件(vue3.x新增) vue3.x 由于函数式组件被定义为纯函数,因此异步组件的定义需要通过将其包装在新的...
    南崽阅读 3,733评论 0 0
  • 创建项目 首先更新vue-cli到最新版本。 项目变化 vue2.x和vue3.x目录结构没有什么变化,只是一些使...
    抽疯的稻草绳阅读 13,435评论 9 69