Vue3—vue-router 常见配置项

一、常见配置项

1.路由重定向

// 路由列表
const routes = [
	{
		path: '/',
		redirect: '/home' // 通过根路由默认跳转home路由
	},
	{
		path: '/home',
		name: 'home',
		component: () => import('../view/home.vue')
	}
]

2. 路由的对应组件静态加载和动态加载

import {createRouter, createWebHashHistory} from 'vue-router';
import test from '../views/test.vue'
// 路由列表
const routes = [
    {
        path: '/',
        name: 'test',
        // 静态加载
        component: test
    },
    {
        path: '/test',
        name: 'test',
        // 动态加载
        component: () => import('../views/test.vue')
    },
]
// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
})

3. 路由模式切换

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    mode: 'history', // 路由模式:hash 路由有#号, history 没有#号
})

history路由效果:http://localhost:3000/
hash路由效果: http://localhost:3000/#/

4. 设置路由的基础路径

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    // 设置路由基础路径
    base: '/vue3/'
})

路由效果: http://localhost:3000/vue3/#/

5.路由激活时的添加的样式

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    linkActiveClass: 'active', // 路由激活时添加的类名
    linkExactActiveClass: 'cur-active' // 链接被精确匹配激活时添加的类名
})
<template>
<router-link to="/promotionApply">promotionApply</router-link>
<router-link to="/inspectorApply">inspectorApply</router-link>
</template>

6. 路由跳转时滚动条的位置

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) { // 路由跳转时滚动条位置
        if (savedPosition) { // 有保存位置
            return savedPosition
        } else {
            return {x: 0, y: 0}
        }
    }
})

7. 路由参数解析和字符串化

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    // 路由参数解析和字符串化, 路由?号后面的参数转换,通常会自动转换,也可以自定义转换
    parseQuery (query) {

    },
    // 路由参数解析和字符串化
    stringifyQuery(obj) {

    }
})

8. 当路由不支持history模式时,自动切换回hash模式

// 导出路由
export default createRouter({
    history: createWebHashHistory(),
    routes,
    fallback: true // 当浏览器不支持 history.pushState 控制路由是否应该回退到 hash 模式。默认值为 true。
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue Router 是 Vue.js 官方的路由管理器,它和 Vue.js 核心深度集成,可以非常方便地实现单页应用程序的路由功能。下面是 Vue Router 的配置以及参数的介绍: ### 安装和引入 首先,我们需要通过 npm 安装 Vue Router: ``` npm install vue-router --save ``` 然后,在 main.js 中引入并使用 Vue Router: ```javascript import VueRouter from 'vue-router' import Vue from 'vue' // 引入路由组件 import Home from './components/Home.vue' import About from './components/About.vue' Vue.use(VueRouter) const router = new VueRouter({ routes: [ { path: '/', component: Home }, { path: '/about', component: About } ] }) new Vue({ el: '#app', router, render: h => h(App) }) ``` ### 配置项 在上面的代码中,我们使用了 VueRouter 的一个配置项 routes,它是一个数组,每个元素都是一个路由对象,包含以下属性: - path:路由的路径。 - component:对应的组件。 除了上面的两个属性外,还有一些其他的属性可以配置: - name:给路由起个名字,方便在代码中进行跳转。 - redirect:重定向到另一个路由。 - children:子路由,可以实现嵌套路由。 - meta:元信息对象,可以在路由钩子函数中使用。 ```javascript const router = new VueRouter({ routes: [ { path: '/', component: Home, name: 'home', meta: { requiresAuth: true } }, { path: '/about', component: About, children: [ { path: 'team', component: Team }, { path: 'history', component: History } ] }, { path: '/login', component: Login }, { path: '/logout', redirect: '/login' } ] }) ``` ### 路由跳转 Vue Router 提供了两种方式进行路由跳转: - 声明式导航:使用 router-link 组件实现。 - 编程式导航:使用 router 对象的 push、replace 和 go 方法实现。 #### 声明式导航 ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` #### 编程式导航 ```javascript // 前进到新的路由 router.push('/about') // 替换当前路由 router.replace('/about') // 后退一步 router.go(-1) ``` ### 路由钩子函数 Vue Router 提供了一些路由钩子函数,可以在路由跳转前、跳转后或者路由变化时执行一些额外的逻辑。常用的钩子函数包括: - beforeRouteEnter:路由进入前,组件还未被创建。 - beforeRouteUpdate:路由更新前,组件已经被创建,但是路由参数发生了变化。 - beforeRouteLeave:路由离开前,组件还未被销毁。 ```javascript const Foo = { template: `...`, beforeRouteEnter (to, from, next) { // 在进入路由前执行 // 此时组件还未被创建,无法访问 this next() }, beforeRouteUpdate (to, from, next) { // 在路由更新前执行 // 此时组件已经被创建,可以访问 this next() }, beforeRouteLeave (to, from, next) { // 在路由离开前执行 // 可以在这里进行一些数据保存操作等 next() } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帝博格T-bag

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值