单页应用程序:
路由介绍
生活中的路由:设备和ip的映射关系
Vue中的路由:路径和组件的映射关系
VueRouter的介绍:
作用:修改地址栏路径时,切换显示匹配的组件
路由的使用步骤5+2
5个基础步骤(固定)
npm add vue-router@3.6.5
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router= new VueRouter()
new Vue({
render: h => h(App),
router:router
}).$mount('#app')
2个核心步骤
//main.js中
import Find from './views/Find.vue'
import My from './views/My.vue'
import Friend from './views/Friend.vue'
const router= new VueRouter({
routes:[
{path:'/find',component:Find},
{path:'/my',component:My},
{path:'/friend',component:Friend},
]
})
//App.vue中
<template>
<div>
<div class="footer_wrap">
<a href="#/find">发现音乐</a>
<a href="#/my">我的音乐</a>
<a href="#/friend">我的朋友</a>
</div>
<div class="top">
<!-- 路由出口,匹配的组件所展示的位置 -->
<router-view></router-view>
</div>
</div>
</template>
.vue组件分类:页面组件(用views文件夹存放-配合路由使用)、复用组件(用components文件夹存放)
目标:将路由模块抽离出来,好处:拆分模块,利于维护
src下新建一个router文件夹内部新建index.js文件,将在main.js中创建的路由对象和引入都剪切进index.js中,并将该文件在main.js中引入,记得修改路径(或直接使用绝对路径)
//index.js
import Find from '../views/Find.vue'//@/vies/Find,用@指代src目录
import My from '../views/My.vue'//@/vies/My
import Friend from '../views/Friend.vue'//@/vies/Friend
import VueRouter from 'vue-router'
Vue.use(VueRouter)//Vue插件初始化
//创建了一个路由对象
const router= new VueRouter({
//routes路由规则们 route一条路由规则
routes:[
{path:'/find',component:Find},
{path:'/my',component:My},
{path:'/friend',component:Friend},
]
})
//main.js
import router from './router/index'
<router-link to=''></router-link>
等于
<a href=''></a>
const router=new Vue({
linkActiveClass:'类名1',//配置模糊匹配类名
linkExactActiveClass:'类名2'//配置精确匹配类名
})
目标:在跳转路由时,进行传值
const router=new VueRouter({
routes:[
{
path:'/search/:words(参数名)',
component:Search
}
]
})
to='/path/参数值'
$route.params.word(参数名)
使用动态路由传参时,在跳转时必须要跟上参数值
如果不传参,但是希望匹配,可以加一个可选符?
即path:‘/search/:words?’
问题:网页打开,url默认是/路径,为匹配到组件时,会出现空白
说明:重定向–>匹配path后,强制跳转path路径
const router=new VueRouter({
routes:[
{path:'/',redirect:'/home'},
{path:'/search/:words(参数名)',component:Search},
{path:'/home',component:Home}
]
})
配在路由最后
const router=new VueRouter({
routes:[
{path:'/',redirect:'/home'},
{path:'/search/:words(参数名)',component:Search},
{path:'/home',component:Home},
{path:'*',component:NotFind}
]
})
路由的路径看起来不自然,有#,用mode:'history’切成真正路径的形式
const router=new VueRouter({
routes:[
{path:'/',redirect:'/home'},
{path:'/search/:words(参数名)',component:Search},
{path:'/home',component:Home},
{path:'*',component:NotFind}
],
mode:'hisrory'
})
两种语法:
this.$router.push('路由路径')
或
this.$router.push({
path:'路由路径'
})
//需要先给路由起名字
const router=new VueRouter({
routes:[
{name:"search",path:'/search/:words(参数名)',component:Search}
],
mode:'hisrory'
})
this.$router.push({
name:'路由名'
})
两种传参方式:查询参数、动态路由传参
两种跳转方式,对于两种传参方式都支持
this.$router.push('/路径?参数名1=参数值1&参数名2=参数值2')
或
this.$router.push({
path:'/路径',
query:{
参数名1:'参数值1',
参数名2:'参数值2'
}
})
获取用$route.query.参数名
this.$router.push('/路径?参数值')
或
this.$router.push({
path:'/路径/参数值'
})
获取用$route.params.参数名
this.$router.push({
name:'路由名字',
query:{
参数名1:'参数值1',
参数名2:'参数值2'
}
})
获取用$route.query.参数名
this.$router.push({
name:'路由名字',
params:{
参数名:'参数值',
}
})
获取用$route.params.参数名
思路:先配置路由后实现功能
配路由步骤
//index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)//Vue插件初始化
//创建了一个路由对象
const router= new VueRouter({
//routes路由规则们 route一条路由规则
routes:[]
})
//main.js
import router from './router/index'
new Vue({
render:h=>h(App),
router
}).$mount('#app')
//App.vue
<template>
<div>
<router-view></router-view>//配置路由出口
</div>
</template>
//index.js
import Layout from '@/views/Layout'
import ArticleDetail from '@/views/ArticleDetail'
const router= new VueRouter({
//routes路由规则们 route一条路由规则
routes:[
{
path:'/',
component:Layout
},//主页部分
{
path:'/detail',
component:ArticleDetail
}//面经详情部分
]
})
//index.js
import Layout from '@/views/Layout'
import ArticleDetail from '@/views/ArticleDetail'
import Article from '@/views/Article'
import Collect from '@/views/Collect'
import Like from '@/views/Like'
import User from '@/views/User'
const router= new VueRouter({
//routes路由规则们 route一条路由规则
routes:[
{
path:'/',
redirect:'/article',//重定向,匹配到path'/'后,强制跳转'/article'
component:Layout,
//通过children配置项,配置嵌套子路由
children:[
{
path:'/article',
component:Article
},
{
path:'/collect',
component:Collect
},
{
path:'/like',
component:Like
},
{
path:'/user',
component:User
}
]
},//主页部分
{
path:'/detail',
component:ArticleDetail
}//面经详情部分
]
})
//Layout.vue
<template>
<div>
<router-view></router-view>//配置二级路由出口
<div>...</div>//底部导航栏
</div>
</template>
实现功能步骤
'/路径?参数名=参数值'
=> this.$route.query.参数名
(更适合多个参数的情况)path:'/路径/:参数名'
=> '/路径/参数值'
(常用反引号+${}
) => this.$route.params.参数名
(更适合单个参数的情况)//只希望LayOut被缓存
<keep-alive :include='LayOutPage'>
<router-view></router-view>
</keep-alive>