Skip to content

Commit

Permalink
✨ feat(utils): 为 unplugin-vue-router 编写 getRouteName 函数,以便于实现自定义的路由名称;
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan-cat committed Oct 16, 2024
1 parent cdf7506 commit 741661b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
14 changes: 12 additions & 2 deletions utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,19 @@
"@ruan-cat/vuepress-preset-config": "workspace:^",
"typedoc": "^0.26.7",
"typedoc-plugin-markdown": "^4.2.7",
"typescript": "catalog:"
"typescript": "catalog:",
"unplugin-vue-router": "^0.10.8"
},
"peerDependencies": {
"typescript": "catalog:"
"typescript": "catalog:",
"unplugin-vue-router": "^0.10.8"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
},
"unplugin-vue-router": {
"optional": true
}
}
}
36 changes: 36 additions & 0 deletions utils/src/unplugin-vue-router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { type Options } from "unplugin-vue-router";

type GetRouteName = NonNullable<Options["getRouteName"]>;

/**
* 自主生成路由名称
* @description
* 对插件自动生成的路由名称,很不满意,不好看,容易引起阅读歧义。
*
* 故自定义。
*
* unplugin-vue-router 插件的 getRouteName 配置项
*/
export const getRouteName: GetRouteName = function _getRouteName(node): ReturnType<GetRouteName> {
// 如果是根节点 那么没有对应的文件夹名称 返回空字符串
if (!node.parent) {
return "";
}

/** 上一次递归产生的字符串 */
const last = _getRouteName(node.parent);

/**
* 路由名称链接符
* @description
* 如果上一次递归产生的字符串为空,则不需要链接符
*/
const connector = last === "" ? "" : "-";

/** 当前节点的路由名称 */
const current = node.value.rawSegment === "index" ? "" : `${connector}${node.value.rawSegment}`;

// 返回上一次递归产生的字符串和当前节点的路由名称的拼接
// 从后面逐步拼接
return last + current;
};

0 comments on commit 741661b

Please sign in to comment.