vue3路由Router的配置和跳转
创始人
2024-06-03 01:37:48
0

文章目录

  • 一、介绍
  • 二、配置
    • 1、配置路由
    • 2、全局挂载
    • 3、使用
  • 三、跳转
    • 1、JS
    • 2、router-link


一、介绍

  • SPA(single page application):单页面应用,只有一个完整的页面;它在加载页面时,不会加载整个页面,而是只更新某个指定的容器中内容。核心之一就是: 更新视图而不重新请求页面
  • vue-router在实现单页面前端路由时,提供了两种方式:Hash模式和History模式
    • hash:默认,hash(#)是URL的锚点,代表的是网页中的一个位置,比如 localhost:8093/#/hello、localhost:8093/#/hi,hash值分别为#/hello、#/hi
      改变#后的部分,浏览器只会滚动到相应位置,不会重新加载网页,因此改变 hash 不会重新
      加载页面
    • history:url中没有#,这种模式充分利用了html5 history interface 中新增的pushState、 replaceState方法,这两个方法应用于浏览器的历史记录栈,在当前已有的 back、forward、go 的基础之上,它们提供了对历史记录进行修改的功能。只是当它们执行修改时,虽然改变了当前的 URL,但浏览器不会立即向后端发送请求
    • 当使用history模式时,需要后端支持(nginx-try_files),否则路由组件页面刷新会404
    • 两者在引用静态资源文件时也有区别
  • 路由,就是指向的意思,主要是构建 SPA (单页应用)时,方便渲染你指定路由对应的组件,比如:请求/hello,就访问hello.vue组件,请求/hi,就访问hi.vue组件
  • SPA只有一个index.html入口url,其他url都是通过前端路由跳转的

二、配置

1、配置路由

  • 路由配置文件:src/router/index.js
  • Router是Vue.js官方的路由管理器,管理routes中定义的路由
//引入:createRouter(Router);createWebHistory(history模式);createWebHashHistory(hash模式)
import { createRouter, createWebHistory } from 'vue-router'//routes是一组路由,把里面每一条路由组合起来,形成一个数组
const routes = [//注册一个路由{//相当于给path取个别名,方便使用,不写默认为default,路由跳转时可以使用name: 'About',//meta:头部信息;title:标签页名称;activeMenu:激活菜单的pathmeta: {title: '首页', activeMenu: '/result/result_survey'},//自定义访问的url,不要包含或等于 vue.config.js 中的proxy//path不能重复,否则[Vue warn]: Duplicate keys detected: '/'. This may cause anupdate errorpath: '/about',/*** 该url指向的组件*  引入组件两种方式*    1、import:webpack打包时路由里的所有component都会打包在一个js中,页面多的话,这个文件会很大,造成进入首页时,需要加载的内容过多,时间相对比较长*      import screen from "@/components/screen"*      component: screen**    2、路由懒加载,两种写法,会将component分别打包成不同的js,加载的时候也是按需加载,只用访问这个路由网址时才会加载这个js*      component: (resolve) => require(['@/components/screen'], resolve)*      component: () => import('@/views/Home.vue')*/component: () => import('@/views/About.vue')},//同一个目录下多个菜单{path: '/qte',component: Layout,hidden: true,children: [{//localhost:8080/system/deptname: 'Dept',meta: {title:'部门列表'},path: 'dept',component: (resolve) => require(['@/views/system/dept/index'], resolve)},{//localhost:8080/system/username: 'User',path: 'user',meta: {title:'用户列表'},component: (resolve) => require(['@/views/system/user/index'], resolve)}]}
]const router = createRouter({//history模式history: createWebHistory(process.env.BASE_URL),routes
})export default router

2、全局挂载

  • 监听地址栏的改变,修改项目中的router-view组件应该加载的组件
  • src/main.js
//引入router
import router from './router'//router实例注入到vue根实例
createApp(App).use(router).mount('#app')

3、使用

  • App.vue

三、跳转

1、JS

//引入路由
import { useRouter } from "vue-router"export default {name: "Screen",setup(){//定义router对象const router=useRouter()//router对象的属性{addRoute:afterEach://返回上一个页面back:() => go(-1)beforeEach:beforeResolve://当前的路由信息currentRoute:{value:{//pathfullPath: "/screen?id=1",hash: "",href: "/screen?id=1",matched: [{…}]//metameta: {title: '大屏'}//namename: "Screen"//paramsparams: {}//pathpath: "/screen"//queryquery: {id: '1'}redirectedFrom: undefined}}//下一个页面forward:() => go(1)getRoutes:go:hasRoute:install:isReady:listening:onError:options:push:removeRoute:replace:resolve:}/*** 1、query* url:http://localhost:8081/screen?id=1*///传参router.push({name:'Screen',query: {id:1}})//接收const id=router.currentRoute.value.query.id/*** 2、params* 路由配置的path:path: '/screen/:id/:name'* url:http://localhost:8081/screen/1/kimi**///传参router.push({name:'Screen',params: {id:1,name:'kimi'}})//接收const name=router.currentRoute.value.params.name//返回上一页面,利用router对象提供的back方法function jumpBack(){router.back()}return{id,name,jumpBack}}
}

2、router-link

  • 也是两种:query、params,和js的传参参数一样

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...