npm i vue-router@3
//该文件专门用于创建整个应用的路由实例对象
import VueRouter from "vue-router";
//引入组件
import About from "@/components/About";
import Home from "@/components/Home";
//创建并暴露一个路由实例对象
export default new VueRouter({
routes: [
{
path: "/about",
component: About,
},
{
path: "/home",
component: Home,
},
],
});
.....
//引入vue-router插件
import VueRouter from "vue-router";
// 引入路由实例对象
import router from "@/router";
//应用插件
Vue.use(VueRouter);
Vue.config.productionTip = false;
new Vue({
render: (h) => h(App),
router,//注册路由实例对象
}).$mount("#app");
<router-link active-class=“active” to = “/home”>Home
roter-link是使用标签,to是跳转地址
routes: [
{
path: "/about",
component: About,
},
{
path: "/home",
component: Home,
children: [//通过children配置子级路由
{
path: "news",//此处一定不要写:/news
component: News,
},
{
path: "messages",//此处一定不要写:/messages
component: Messages,
},
],
},
],
// 路径要带着父级路径
<router-link active-class="active" to="/home/news">NEWSrouter-link>
<!-- 跳转路由并携带query参数,to的字符串写法-->
<router-link :to="`/home/messages/detail?id=${m.id}&title=${m.title}`">{{ m.title }}</router-link>
<!-- 跳转路由并携带query参数,to的对象写法(推荐)-->
<router-link :to="{
path:'/home/messages/detail',
query:{
id:m.id,
title:m.title
}
}">{{ m.title }}</router-link>
$route.query.id
$route.query.title
{
path: "/home",
component: Home,
children: [
{
path: "news",
component: News,
},
{
path: "messages",
component: Messages,
children: [
{
name: "xiangqing",
path: "detail/:id/:title", //使用占位符声明接收params
component: Detail,
},
],
},
],
},
<router-link :to="`/home/messages/detail/${m.id}/${m.title}`">
{{ m.title }}
router-link>
<router-link :to="{
name:'xiangqing',//只能用name不能用path路径写法
params:{
id:m.id,
title:m.title
}
}">
{{ m.title }}
router-link>
特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path的路径写法配置,必须使用name配置!
$route.parmams.id
$route.params.title
routes: [
{
name: "guanyu",//给路由命名
path: "/about",
component: About,
},]
//简化前
<router-link to="/about">Aboutrouter-link>
//简化后
<router-link :to="{name:'guanyu'}">Aboutrouter-link>
//简化写法配合传递参数
<router-link
:to="{
name:'guanyu',
query:{
id:666,
title:'传参'
}
}">Aboutrouter-link>
children: [
{
name: "xiangqing",
path: "detail/:id/:title", //params需要设置传参,query不用
component: Detail,
//props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件.
props:{a:1,b:'hello'}//写死的传递数据,不常用
//props的第二种写法,值为布尔值,如果为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件.
props: true,
//props的第三种写法,值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件.
//()括号中为解构赋值$route拿到query再解构赋值拿到id和title,es6写法
props({ query: { id, title } }) {
return { id: id, title: title };
},
},
],
push
和replace
,push是追加历史,replace是替换当前记录,路由跳转的时候默认是push
replace
模式:
跳转
methods: {
pushShow(m) {
// $router的两个API
this.$router.push({
name: 'xiangqing',//只能用name不能用path路径写法
params: {
id: m.id,
title: m.title
}
})
},
replaceShow(m) {
this.$router.replace({
name: 'xiangqing',//只能用name不能用path路径写法
params: {
id: m.id,
title: m.title
}
})
},
//后退
back() {
this.$router.back()
},
//前进
forward() {
this.$router.forward()
},
//传递参数,正数前进n步,负数后退n步
go() {
this.$router.go(number)
},
}
<!--include只缓存某一组件(要写组件的name),不写则全部缓存-->
<keep-alive include="News">
<!--如果想要缓存其中两个或四个而不全部缓存,需要写成数组形式,且要加:-->
<keep-alive :include="['News','Messages']">
<router-view></router-view>
</keep-alive>