路由:路由是指路由器从一个接口上收到数据包,根据数据包的目的地址进行定向并转发到另一个接口的过程。
1.路由切换
<router-link to=“/user">
to="/user" 切换路由的地址
路由配置
/router/index
普通
{
path:"/user",
name:'user',
component:()=>improt(xxx)
}
传参
{
path:"/product/:id",
name:'product',
component:()=>improt(xxx)
}
<router-link to="/product/abc">
在页面中获取
$route.params.id
<!-- $route当前路由 params路由参数 id -->
404
要配置在最后面
{
path:"*"
}
编程式路由跳转
$router
前进
$router.forward()
$router.go(1)
后退
$router.back()
$router.go(-1)
切换路由
$router.push("/about")
替换路由(不留当前页面的历史记录)
$router.replace("/about")
路由信息 $route
路由参数 params
查询 query
地址 path
全地址 fullPath
名称 name
哈希值 hash
路由守卫
//路由守卫组件内部
//进入前
beforeRouterEnter(to,from,next)
//to要进入的路由
from //从哪个页面进入
next //下一步
next() //进入to页面
next(true) //进入to页面
next(false) //不跳转
next("/login") //跳转到登录
//离开前
beforeRouterLeave
//更新前
beforeRouterUpdate
//全局
beforeEach((to,from,next)=>{})
beforeEnter()
//进入离开页面前做拦截,处理事情(跳转提示,权限检查)