注释很详细,直接上代码
新增内容
404匹配处理方法
源码
src/router/index.js
//导入所需模块
import Vue from "vue";
import VueRouter from "vue-router";
import myMusic from "@/views/myMusic.vue";
import findMusic from "@/views/findMusic.vue";
import attentionSigner from "@/views/attentionSigner.vue";
import recommendList from "@/views/recommendList.vue";
import rankingList from "@/views/rankingList.vue";
import songList from "@/views/songList.vue";
import notFound from "@/views/notFound.vue";
//调用函数将VueRouter插件安装为Vue的插件
Vue.use(VueRouter);
//配置路由规则
const routes = [
//重定向
{ path: "/", redirect: "/MyMusic/recommendList" },
{
path: "/myMusic",
component: myMusic,
// 二级路由无需写'/'
children: [
{
path: "recommendList",
component: recommendList,
},
{
path: "rankingList",
component: rankingList,
},
{
path: "songList",
component: songList,
},
],
},
{
// 动态路由接受参数(后面加问号表示为可选参数)
path: "/findMusic/:name?",
component: findMusic,
},
{
path: "/attentionSigner",
component: attentionSigner,
},
{
path: "/songList/",
component: songList,
},
{
path: "/rankingList/",
component: rankingList,
},
{
path: "/recommendList/",
component: recommendList,
},
{
path: "/attentionSigner/",
component: attentionSigner,
},
// 404的情况(*通配符,放在最后匹配其他所有情况)
{
path:"*",
component:notFound
}
];
//创建路由实例
const router = new VueRouter({
// 路由配置
routes,
//历史模式
mode: 'history'
//这里可以修改router-link的默认类名
/*
linkActiveClass:'my-active-class',
linkExactActiveClass:'my-exact-active-class'
*/
});
//导出路由实例
export default router;
src/views/notFound.vue
<template>
<div>
<h1>404</h1>
</div>
</template>
<script>
export default {
}
</script>
<style lang="less" scoped>
h1{
font-size: 200px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
font-family: '宋体';
}
</style>
效果演示