# 侧边栏路由需要优化
侧边栏的路由需要一个一个手动配置,很麻烦,所以写了一个函数,根据目录生成对应的路由
nav: [
{ text: "主页", link: "/" },
{ text: "关于我", link: "/about/jianli.html" },
{ text: "项目经验", link: "https://github.com/zengbotao" },
],
sidebar: [
{
title: "主页", // 必要的
path: "/", // 可选的, 标题的跳转链接,应为绝对路径且必须存在
},
{
title: "博客搭建",
collapsable: false,
children: getCatalog('construction')
},
# 路由生产函数
function getList(catalog){
//commonJs http://javascript.ruanyifeng.com/nodejs/module.html#toc13
//es6 https://es6.ruanyifeng.com/#docs/async
// path的常见语法 https://blog.csdn.net/weixin_43285360/article/details/121478210
// fs https://blog.csdn.net/Piconjo/article/details/105597723
var fs = require('fs');
var path=require('path');
var filePath = path.resolve(__dirname,catalog)
let fileList=fs.readdirSync(filePath) //必须要同步读取目录
let data=fileList.map((item)=>{
let title=path.basename(item,'.md')
let paths=`/${catalog}/`+title+'.html'
return {
title:title,
path:paths
}})
// console.log(data,filePath)
return data
}
//只能用module.exports
module.exports = getList
# config的配置优化
以后就可以直接再对应的文件夹下添加文件,不必每次都更新路由,省时省力
let getCatalog=require('../getCatalog')
module.exports = {
title: "zengbotao",
// base: "/docs/",
plugins: [
[
"@vuepress/pwa",
{
serviceWorker: true,
updatePopup: {
message: "New content is available.",
buttonText: "Refresh",
},
},
],
[
"@vssue/vuepress-plugin-vssue",
{
// 设置 `platform` 而不是 `api`
platform: "github-v4",
// 其他的 Vssue 配置
owner: "zengbotao",
repo: "docs",
clientId: "ab4f92230febd748188f",
clientSecret: "4df9463808c8b8775b3e3b6c02c9cd64503f4835",
},
],
],
themeConfig: {
author: "zengbotao",
searchPlaceholder: "Search",
searchMaxSuggestions: 10, // Search 建议列表条目数
logo: "/assets/img/logo.png",
lastUpdated: "Last Updated",
smoothScroll: true,
nav: [
{ text: "主页", link: "/" },
{ text: "关于我", link: "/about/jianli.html" },
{ text: "项目经验", link: "https://github.com/zengbotao" },
],
sidebar: [
{
title: "主页", // 必要的
path: "/", // 可选的, 标题的跳转链接,应为绝对路径且必须存在
},
{
title: "博客搭建",
children: getCatalog('construction')
},
{
title: "项目笔记",
children: [
{
title: "vue", // 必要的
collapsable: false,
children: getCatalog('vue')
},
{
title: "react", // 必要的
collapsable: false,
children: getCatalog('react')
},
{
title: "webpack", // 必要的
collapsable: false,
children: getCatalog('webpack')
},
{
title: "vite", // 必要的
collapsable: false,
children: getCatalog('vite')
},
{
title: "网页优化", // 必要的
collapsable: false,
children:getCatalog('note')
},
],
},
{
title: "学习笔记",
children: getCatalog('webhexin')
},
],
},
};