vue3 vue-router 笔记

news/2024/10/23 5:41:01/

1.安装

 npm install vue-router

2.创建router文件,在main.js引入,创建home.about页面 app.vue文件添加路由占位符
router/index.js

  import { createRouter,createWebHashHistory, createWebHistory } from 'vue-router'import home from './views/home.vue'import about from '../views/about.vue'const routes = [{path:'/home',component: home},{path:'/about',component: about}]const router = createRouter({// hash 模式跟 history 模式history: createWebHashHistory(),routes})export default router

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from "./route/index"
const app = createApp(App)
app.use(router)
app.mount('#app')

app.vue

 <!-- replace 替换路径,不会回退 --><router-link to="/home" replace active-class="active">首页</router-link><router-link to="/about" active-class="active">关于</router-link><router-view></router-view>

3.路由懒加载,redirect 从定向

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'// 路由懒加载
// const home = () => import(/* webpackChunkName :'home'*/'../views/home.vue')
// const about = () => import(/* webpackChunkName :'about'*/'../views/about.vue')
const routes = [{path: '/',redirect: '/home'},{path: '/home',name: 'home',component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),},{path: '/about',name: 'about',component: () => import(/* webpackChunkName :'about'*/'../views/about.vue')}
]
const router = createRouter({history: createWebHistory(),routes
})
export default router

4.动态路由 创建user.vue文件
router/index.js

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [// 动态路由{path: '/user/:id',name: 'user',component: () => import('../views/user.vue')}
]
const router = createRouter({history: createWebHistory(),routes
})
export default router

app.vue

 <router-link to="/user/123" active-class="active">用户123</router-link><router-link to="/user/456" active-class="active">用户456</router-link>

user.vue

<div>user{{ $route.params.id }}</div>

5.404页面处理,创建404文件
使用 pathMatch

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [// 404 页面不存在路由  后面加* 会解析输入的路径,不加则不解析{path: '/:pathMatch(.*)*',component: () => import('../views/404.vue')}
]
const router = createRouter({history: createWebHistory(),routes
})
export default router

6.路由嵌套 创建详情1,2 文件

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [{path: '/',redirect: '/home'},{path: '/home',name: 'home',component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),children: [{path:'/home',redirect:'/home/detailOne'},{path: 'detailOne', //等同于 /home/detailOnecomponent: () => import('../views/detailOne.vue')},{path:'detailTwo',component:()=> import('../views/detailTwo.vue')}]}
]
const router = createRouter({history: createWebHistory(),routes
})
export default router

7.编程式跳转
app.vue 添加点击事件

<hr /><span @click="homeBtnClick">首页</span><button @click="aboutBtnClick">关于</button><button @click="goBack">返回</button><router-view></router-view>

添加事件方法 query 传参

<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
function homeBtnClick() {router.push("/home");
}
function aboutBtnClick() {router.push({path: "/about",query: {name: "言念",age: 24,},});
}
function goBack() {router.back();router.go(-1);
}
</script>

about.vue

<template><div class="about">about<h1>{{$route.query}}</h1></div>
</template>
<script setup>
</script>
<style scoped></style>

8.完整router/index.js

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// import home from '../views/home.vue'
// import about from '../views/about.vue'// 路由懒加载
// const home = () => import(/* webpackChunkName :'home'*/'../views/home.vue')
// const about = () => import(/* webpackChunkName :'about'*/'../views/about.vue')
const routes = [{path: '/',redirect: '/home'},{path: '/home',name: 'home',component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),children: [{path:'/home',redirect:'/home/detailOne'},{path: 'detailOne', //等同于 /home/detailOnecomponent: () => import('../views/detailOne.vue')},{path:'detailTwo',component:()=> import('../views/detailTwo.vue')}]},{path: '/about',name: 'about',component: () => import(/* webpackChunkName :'about'*/'../views/about.vue')},// 动态路由{path: '/user/:id',name: 'user',component: () => import('../views/user.vue')},// 404 页面不存在路由  后面加* 会解析输入的路径,不加则不解析{path: '/:pathMatch(.*)*',component: () => import('../views/404.vue')}
]
const router = createRouter({history: createWebHistory(),routes
})
export default router

9.完整的app.vue

<template><div class="app"><h1>app content</h1><!-- replace 替换路径,不会回退 --><router-link to="/home" replace active-class="active">首页</router-link><router-link to="/about" active-class="active">关于</router-link><router-link to="/user/123" active-class="active">用户123</router-link><router-link to="/user/456" active-class="active">用户456</router-link><hr /><span @click="homeBtnClick">首页</span><button @click="aboutBtnClick">关于</button><button @click="goBack">返回</button><router-view></router-view></div>
</template><script setup>
import { useRouter } from "vue-router";
const router = useRouter();
function homeBtnClick() {router.push("/home");
}
function aboutBtnClick() {router.push({path: "/about",query: {name: "言念",age: 24,},});
}
function goBack() {router.back();router.go(-1);
}
</script>
<style>
.active {color: red;font-size: 20px;
}
</style>

http://www.ppmy.cn/news/1230159.html

相关文章

记一次线上bug排查-----SpringCloud Gateway组件 请求头accept-encoding导致响应结果乱码

基于公司的业务需求&#xff0c;在SpringCloud Gateway组件的基础上&#xff0c;写了一个转发服务&#xff0c;测试开发阶段运行正常&#xff0c;并实现初步使用。但三个月后&#xff0c;PostMan请求接口&#xff0c;返回异常&#xff0c;经排查&#xff0c;从日志中获取到转发…

C++学习 --pair

目录 1&#xff0c; 什么是pair 2&#xff0c; 创建pair 2-1&#xff0c; 标准数据类型 2-2&#xff0c; 自定义数据类型 3&#xff0c; 查询元素 3-1&#xff0c; 标准数据类型 3-2&#xff0c; 自定义数据类型 1&#xff0c; 什么是pair 数据以键值对形式存放的容器&…

Web安全研究(五)

Automated WebAssembly Function Purpose Identification With Semantics-Aware Analysis WWW23 文章结构 introbackgroundsystem design abstraction genapplying abstractionsclassifier data collection and handling data acquisitionstatistics of collected datamodule-…

postgresql安装fdw扩展

最近有同一个服务器不同数据库、不同服务器数据库之间的数据同步需求&#xff0c;使用了fdw 下面举例的是同一个服务器两个不同数据库的同步情况 1、安装扩展 create extension postgres_fdw; 在需要使用fdw的数据库都加上该扩展 2、创建fdw服务器 mlhbase_prd库 CREATE…

基于数据库(MySQL)与缓存(Redis)实现分布式锁

分布式锁 分布式锁&#xff1a;分布式锁是在分布式的情况下实现互斥类型的一种锁 实现分布式锁需要满足的五个条件 可见性&#xff1a;多个进程都能看到结果互斥性&#xff1a;只允许一个持有锁的对象的进入临界资源可用性&#xff1a;无论何时都要保证锁服务的可用性&#x…

Hive效率优化记录

Hive是工作中常用的数据仓库工具&#xff0c;提供存储在HDFS文件系统&#xff0c;将结构化数据映射为一张张表以及提供查询和分析功能。 Hive可以存储大规模数据&#xff0c;但是在运行效率上不如传统数据库&#xff0c;这时需要懂得常见场景下提升存储或查询效率的方法&#x…

最强英文开源模型Llama2架构与技术细节探秘

prerequisite: 最强英文开源模型LLaMA架构探秘&#xff0c;从原理到源码 Llama2 Meta AI于2023年7月19日宣布开源LLaMA模型的二代版本Llama2&#xff0c;并在原来基础上允许免费用于研究和商用。 作为LLaMA的延续和升级&#xff0c;Llama2的训练数据扩充了40%&#xff0c;达到…

gitlab利用CI多工程持续构建

搭建CI的过程中有多个工程的时候&#xff0c;一个完美的构建过程往往是子工程上的更新(push 或者是merge)触发父工程的构建&#xff0c;这就需要如下建立一个downstream pipeline 子仓库1 .gitlab-ci.yml stages:- buildbuild_job:stage: buildtrigger:project: test_user/tes…