Angular的ngRouter
React的ReactRouter
Vue的vue-router
Vue Router 是Vue.js的官方路由:
vue-router是基于路由和组件的
安装Vue Router
npm install vue-router
App.vue
<template>
<div>
<div>appdiv>
<router-link to="/home">首页router-link>
<router-link to="/about">关于router-link>
<router-view>router-view>
div>
template>
<script>
script>
<style>
style>
router/index.js
import { createRouter,createWebHashHistory,createWebHistory } from "vue-router";
import Home from '../Views/Home.vue'
import About from '../Views/About.vue'
// 创建一个路由:映射关系
const router = createRouter({
// 指定采用的模式 hash
// history:createWebHashHistory(),
// 使用history模式
history:createWebHistory(),
routes:[
{path:'/',redirect:'/home'},
{
path:"/home",
component:Home
},
{
path:"/about",
component:About
}
]
})
export default router
Home.vue
<template>
<div>
home
div>
template>
<script>
export default {
// name:"Home"
}
script>
<style scoped>
style>
About.vue
<template>
<div>
About
div>
template>
<script>
export default {
}
script>
<style scoped>
style>
// 指定采用的模式 hash
history:createWebHashHistory(),
// 使用history模式
history:createWebHistory(),
<router-link to="/about" replace>关于router-link>
<router-link :to="{path:'/about'}" replace>关于router-link>
.router-link-active{
color: red;
font-size: 30px;
}
设置默认选中class (active-class)
<router-link :to="{path:'/about'}" active-class="link-active">关于router-link>
分包处理
const Home = () => import("../Views/Home.vue");
const About = () => import("../Views/About.vue");
分包处理后,不知道那个文件编译打包的,我们可以用魔法注释 (/ webpackChunkName:‘about’ /)
const Home = () => import(/* webpackChunkName:'home' */"../Views/Home.vue");
const About = () => import(/* webpackChunkName:'about' */"../Views/About.vue");
在template中获取
{{ $route.params.id }}
在compistion-api中获取
<script setup>
import { useRoute,onBeforeRouteUpdate } from 'vue-router';
const route = useRoute();
console.log(route.params.id)
// 获取route跳转id (路由守卫)
onBeforeRouteUpdate((to,from)=>{
console.log("from:",from.params.id)
console.log("to:",to.params.id)
})
</script>
在options-api中获取id
this.$route.params.id
router/index.js
{
path:"/:pathMatch(.*)*",
component:()=>import('../Views/Notfound.vue')
}
Notfound.vue
<template>
<div>
您的页面没找到{{ $route.params.pathMatch }}
div>
template>
<script>
export default {
}
script>
<style scoped>
style>
<template>
<span @click="go_home">首页span>
<button @click="go_about">关于 button>
template>
<script setup>
import { useRouter } from 'vue-router';
const router = useRouter()
// 监听元素点击
function go_home(){
// 调整到首页
// router.push('/home')
router.push({
// name:"home"
path:'/home'
})
}
function go_about(){
// 调整到关于
router.push({
path:'/about',
query:{
name:'why',
age:18
}
})
}
script>
options-api写法
<script>
export default{
methods:{
go_home:function(){
// 调整到首页
// router.push('/home')
this.$router.push({
// name:"home"
path:'/home'
})
},
go_about:function(){
// 调整到关于
this.$router.push({
path:'/about',
query:{
name:'why',
age:18
}
})
}
}
}
script>
页面的前进和后退
<script setup>
import { useRouter } from 'vue-router'
const router = useRouter();
function back(){
// router.back()
// router.forward()
// go(delta)
// go(1) -> forward();
// go(-1) -> back()
router.go(-1)
}
script>
动态添加路由
router/index.js
let isAdmin = true
if(isAdmin){
// 一级路由
router.addRoute({
path:"/admin",
component:()=>import("../Views/Admin.vue")
})
// 添加vip页面
router.addRoute("home",{
path:"vip",
component:()=>import('../Views/HomeVip.vue')
})
}
删除路由有以下三种方式:
vue-router提供的导航守卫主要用来通过跳转或取消的方式守卫导航。
全局的前置守卫beforeEach是在导航触发时会被回调的:
它有两个参数:
它有返回值:
Vue-Router还提供了很多其他守卫函数,目的都是在某一个时刻给予我们回调,让我们可以更好的控制程序的流程或者功能:
https://next.router.vuejs.org/zh/guide/advanced/navigation-guards.html
我们一起来看一下完整的导航解析流程:
感谢大家观看,我们下次再见