熟悉一个项目可以通过router文件
基本结构
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(),// createWebHashHistory()
routes://路由信息对象数组
})
//暴露出去,在main.js中挂载
export default router
路由模式
在创建路由器实例时,history配置允许我们在不同的历史模式中进行选择
hash模式
原理:通过 hashchange 事件监听 hash 值的改变来渲染页面对应的组件
hash模式是用createWebHashHistory()
它在内部传递的实际url之前使用了一个哈希字符(#)。
由于这部分URL从未被发送到服务器,所以它不需要在服务器层面进行任何的处理。
不过,它在SEO(搜索引擎优化)中会有不好的影响
history模式
原理: 利用H5的 history中新增的两个API pushState() 和 replaceState() 和一个事件onpopstate监听URL变化
history模式,也称HTML5模式,用createWebHistory创建
使用这种模式,URL看起来会很正常
问题:应用是一个单页的用户端应用,如果没有适当的服务器配置,用户在浏览器中直接访问,会得到一个404错误
解决:在服务器上添加一个简单的回退路由,如果URL不匹配任何静态资源,它应提供与应用程序中的index.html相同的页面
路由信息对象数组
路由信息对象组成的数组
常用的属性:path、name、component、redirect、children,meta
示例
const newRoutes = [{
path:'/',
name:'Index',
component:Layout,//这样用的要在上面import
redirect:'/home',
children:[
{
path:'/home',
name:'Home',
componemt:()=>import('@/views/Home/Home.vue')//路由懒加载
meta:{title:'XXX',icon:'XXX',...}
},
...
]
}]
path属性:
如果写path:‘/home’,URL为例如http://localhost:8080/#/home
如果写path:‘/test1’,它的children的path:‘/subTest’,URL为例如http://localhost:8080/#/subTest
如果写path:‘/test2’,它的children的path:‘subTest’,URL为例如http://localhost:8080/#/test2/subTest
总结:如果是’/‘开始就是从根路由开始,如果开始不带’/',则从当前路由开始
name属性:
相当于给path起了一个别名,方便使用,非必须,默认default
使用场景:
1、路由跳转使用params传参
2、获取组件的name值使用
3、同个路由,渲染多个视图
路由跳转(带参)
注
//vue3composition:useRoute()、useRouter()方法来获取到route和router
import { useRoute, useRouter } from 'vue-router'
const route = useRoute();
const router = useRouter();
//下面代码通过$获取router或route,vue3composition用这即可
1、router-link
1. 不带参数
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name
2.带参数 params
<router-link :to="{name:'home', params: {id:1}}">
// params传参数 (类似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可请求,刷新页面id会消失,配置path,刷新页面id会保留
// html 取参 $route.params.id
// script 取参 this.$route.params.id
3.带参数 query
<router-link :to="{name:'home', query: {id:1}}">
// query传参数 (类似get,url后面会显示参数)
// 路由可不配置
// html 取参 $route.query.id
// script 取参 this.$route.query.id
2、this.$router.push()
1. 不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2. query传参
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
// html 取参 $route.query.id
// script 取参 this.$route.query.id
3. params传参
this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可请求,刷新页面id会消失,配置path,刷新页面id会保留
// html 取参 $route.params.id
// script 取参 this.$route.params.id
4. query和params区别
query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传
params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失
3、this.$router.replace() (用法同push)
push与replace的区别
push跳转到指定url路径,并向history栈中添加一个记录,点击后退会返回到上一个页面
replace跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (即替换)
4、this.router.go(n)以及this.router.back()
go(-1): 原页面表单中的内容会丢失;
this.$router.go(-1):后退+刷新;
this.$router.go(0):刷新;
this.$router.go(1) :前进
back(): 原页表表单中的内容会保留;
this.$router.back():后退 ;
this.$router.back(0) 刷新;
this.$router.back(1):前进