Skip to content

Commit 5eab1ef

Browse files
docs: add route related docs (#26)
Co-authored-by: meteorlxy <[email protected]>
1 parent 5f57b6c commit 5eab1ef

File tree

8 files changed

+162
-2
lines changed

8 files changed

+162
-2
lines changed

docs/.vuepress/configs/sidebar/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const sidebarEn: SidebarConfig = {
3939
'/advanced/cookbook/making-a-theme-extendable.md',
4040
'/advanced/cookbook/passing-data-to-client-code.md',
4141
'/advanced/cookbook/markdown-and-vue-sfc.md',
42+
'/advanced/cookbook/resolving-routes.md',
4243
],
4344
},
4445
],

docs/.vuepress/configs/sidebar/zh.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const sidebarZh: SidebarConfig = {
3939
'/zh/advanced/cookbook/making-a-theme-extendable.md',
4040
'/zh/advanced/cookbook/passing-data-to-client-code.md',
4141
'/zh/advanced/cookbook/markdown-and-vue-sfc.md',
42+
'/zh/advanced/cookbook/resolving-routes.md',
4243
],
4344
},
4445
],
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Resolving Routes
2+
3+
## Getting all routes
4+
5+
You can make use of [useRoutes](../../reference/client-api.md#useroutes) to get all routes information.
6+
7+
The return value of `useRoutes` is a Ref object containing all routes. The keys are route paths of each route, and the values are the corresponding route information.
8+
9+
```ts
10+
import { useRoutes } from 'vuepress/client'
11+
12+
const routes = useRoutes()
13+
// {
14+
// '/': { meta: { title: 'Home' }, loader: HomePageLoader },
15+
// '/404.html': { meta: { title: 'Not Found' }, loader: NotFoundPageLoader },
16+
// ...
17+
// }
18+
19+
const routePaths = computed(() => Object.keys(routes.value))
20+
// => ['/‘, '/404.html', '/foo/', '/bar/', ...]
21+
```
22+
23+
## Resolving route path
24+
25+
You can make use of [resolveRoutePath](../../reference/client-api.md#resolveroutepath) to resolve the route path of the given link.
26+
27+
`resolveRoutePath` receives a link and an optional base path, and returns the resolved route path:
28+
29+
```ts
30+
import { resolveRoutePath } from 'vuepress/client'
31+
32+
const routePath = resolveRoutePath('/foo/') // => '/foo/'
33+
const routePath = resolveRoutePath('baz.html', '/foo/bar.html') // => '/foo/baz.html'
34+
```
35+
36+
## Resolving route information
37+
38+
You can make use of [resolveRoute](../../reference/client-api.md#resolveroute) to resolve route information for a given link.
39+
40+
`resolveRoute` receives a link and an optional base path, and returns the resolved route information:
41+
42+
```ts
43+
import { resolveRoute } from 'vuepress/client'
44+
45+
const route = resolveRoute('/foo/') // => { notFound: false, path: '/foo/', meta: { title: 'Foo' }, loader: FooPageLoader }
46+
const route = resolveRoute('baz.html', '/foo/bar.html') // => { notFound: false, path: '/foo/baz.html', meta: { title: 'Baz' }, loader: BazPageLoader }
47+
const route = resolveRoute('/not-exist.html') // => { notFound: true, path: '/not-exist.html', meta: { title: 'Not Found' }, loader: NotFoundPageLoader }
48+
```
49+
50+
There is a `notFound` field in the returned information, which is used to indicate whether a corresponding route exists for a given path. When the route does not exist, the `notFound` field would be `true`, the `path` field would be the normalized path, and the `meta` and `loader` fields would point to the default 404 page.

docs/guide/migration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ Some major breaking changes:
415415
- `themeConfig` is removed from user config and site data. To access the `themeConfig` as you would via `this.$site.themeConfig` in v1, we now recommend using the [@vuepress/plugin-theme-data](https://ecosystem.vuejs.press/plugins/theme-data.html) plugin and its `useThemeData` composition API.
416416
- Stylus is no longer the default CSS pre-processor, and the stylus palette system is not embedded. If you still want to use similar palette system as v1, [@vuepress/plugin-palette](https://ecosystem.vuejs.press/plugins/palette.html) may help.
417417
- Markdown code blocks syntax highlighting by Prism.js is not embedded by default. You can use either [@vuepress/plugin-prismjs](https://ecosystem.vuejs.press/plugins/prismjs.html) or [@vuepress/plugin-shiki](https://ecosystem.vuejs.press/plugins/shiki.html), or implement syntax highlighting in your own way.
418-
- For scalability concerns, `this.$site.pages` is not available any more.
418+
- For scalability concerns, `this.$site.pages` is not available any more. See [Advanced > Cookbook > Resolving Routes](../advanced/cookbook/resolving-routes.md) for how to retrieve pages data in v2.
419419

420420
For more detailed guide about how to write a theme in v2, see [Advanced > Writing a Theme](../advanced/theme.md).
421421

docs/reference/client-api.md

+29
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ const {
7373

7474
The value is the `lang` property of the page data.
7575

76+
## useRoutes
77+
78+
- Details:
79+
80+
Returns the routes ref object.
81+
82+
The value is the `routes` property of the site data.
83+
84+
- Also see:
85+
- [Advanced > Cookbook > Resolving Routes](../advanced/cookbook/resolving-routes.md)
86+
7687
### useRouteLocale
7788

7889
- Details:
@@ -106,6 +117,24 @@ const {
106117
- Also see:
107118
- [Advanced > Cookbook > Usage of Client Config](../advanced/cookbook/usage-of-client-config.md)
108119

120+
### resolveRoute
121+
122+
- Details:
123+
124+
Parses the route of the given link.
125+
126+
- Also see:
127+
- [Advanced > Cookbook > Resolving Routes](../advanced/cookbook/resolving-routes.md)
128+
129+
## resolveRoutePath
130+
131+
- Details:
132+
133+
Parses the route path of the given link.
134+
135+
- Also see:
136+
- [Advanced > Cookbook > Resolving Routes](../advanced/cookbook/resolving-routes.md)
137+
109138
### withBase
110139

111140
- Details:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 解析路由
2+
3+
## 获取全部路由
4+
5+
在开发主题和插件时,你可能希望获取所有页面的信息。通过 [useRoutes](../../reference/client-api.md#useroutes) 就可以获取所有页面的路由记录。
6+
7+
`useRoutes` 的返回值是一个包含了所有路由信息的 Ref 对象。其属性是每条路由的路由路径,对应的值是路径的路由信息。
8+
9+
```ts
10+
import { useRoutes } from 'vuepress/client'
11+
12+
const routes = useRoutes()
13+
// {
14+
// '/': { meta: { title: 'Home' }, loader: HomePageLoader },
15+
// '/404.html': { meta: { title: 'Not Found' }, loader: NotFoundPageLoader },
16+
// ...
17+
// }
18+
19+
const routePaths = computed(() => Object.keys(routes.value))
20+
// => [’/‘, '/404.html', '/foo/', '/bar/', ...]
21+
```
22+
23+
## 解析路由地址
24+
25+
你可以使用 [resolveRoutePath](../../reference/client-api.md#resolveroutepath) 来解析给定的链接对应的路由路径。
26+
27+
`resolveRoutePath` 接收一个链接地址和一个可选的基础路径,返回一个解析后的路由地址:
28+
29+
```ts
30+
import { resolveRoutePath } from 'vuepress/client'
31+
32+
const routePath = resolveRoutePath('/foo/') // => '/foo/'
33+
const routePath = resolveRoutePath('baz.html', '/foo/bar.html') // => '/foo/baz.html'
34+
```
35+
36+
## 解析路由信息
37+
38+
你可以使用 [resolveRoute](../../reference/client-api.md#resolveroute) 来解析给定的链接对应的路由信息。
39+
40+
`resolveRoute` 接收一个链接地址和一个可选的基础路径,返回一个解析后的路由信息:
41+
42+
```ts
43+
import { resolveRoute } from 'vuepress/client'
44+
45+
const route = resolveRoute('/foo/') // => { notFound: false, path: '/foo/', meta: { title: 'Foo' }, loader: FooPageLoader }
46+
const route = resolveRoute('baz.html', '/foo/bar.html') // => { notFound: false, path: '/foo/baz.html', meta: { title: 'Baz' }, loader: BazPageLoader }
47+
const route = resolveRoute('/not-exist.html') // => { notFound: true, path: '/not-exist.html', meta: { title: 'Not Found' }, loader: NotFoundPageLoader }
48+
```
49+
50+
路由信息中存在一个 `notFound` 字段,用于标识给定的路径是否存在对应的路由。当路由不存在时,返回值中的 `notFound` 字段为 `true`,其 `path` 字段为规范化后的路径,而 `meta``loader` 字段则会指向默认的 404 页面。

docs/zh/guide/migration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ v1 的主题和插件和 v2 并不兼容。
417417
- `themeConfig` 已经从用户配置和站点数据中移除。如果你想要像 v1 一样通过 `this.$site.themeConfig` 来访问 `themeConfig` ,我们现在建议使用 [@vuepress/plugin-theme-data](https://ecosystem.vuejs.press/zh/plugins/theme-data.html) 插件和它提供的 Composition API `useThemeData`
418418
- Stylus 不再是默认的 CSS 预处理器,并且 Stylus 调色板系统不再被默认支持。如果你仍然想要使用和 v1 类似的调色板系统,可以使用 [@vuepress/plugin-palette](https://ecosystem.vuejs.press/zh/plugins/palette.html)
419419
- 由 Prism.js 提供的 Markdown 代码块的语法高亮不再被默认支持。你可以选择使用 [@vuepress/plugin-prismjs](https://ecosystem.vuejs.press/zh/plugins/prismjs.html)[@vuepress/plugin-shiki](https://ecosystem.vuejs.press/zh/plugins/plugin/shiki.html) ,或者用你自己的方式实现语法高亮。
420-
- 考虑到可扩展性, `this.$site.pages` 不再可用。
420+
- 考虑到可扩展性, `this.$site.pages` 不再可用。查看 [深入 > Cookbook > 解析路由](../advanced/cookbook/resolving-routes.md) 了解如何在 v2 中获取页面的数据。
421421

422422
你可以参考 [深入 > 开发主题](../advanced/theme.md) 来了解如何开发一个 v2 主题。
423423

docs/zh/reference/client-api.md

+29
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ const {
7373

7474
它的值是页面数据的 `lang` 属性。
7575

76+
### useRoutes
77+
78+
- 详情:
79+
80+
返回所有路由的 Ref 对象。
81+
82+
它的值是站点数据的路由信息。
83+
84+
- 参考:
85+
- [深入 > Cookbook > 解析路由](../advanced/cookbook/resolving-routes.md)
86+
7687
### useRouteLocale
7788

7889
- 详情:
@@ -106,6 +117,24 @@ const {
106117
- 参考:
107118
- [深入 > Cookbook > 客户端配置的使用方法](../advanced/cookbook/usage-of-client-config.md)
108119

120+
### resolveRoute
121+
122+
- 详情:
123+
124+
解析给定链接对应的路由
125+
126+
- 参考:
127+
- [深入 > Cookbook > 解析路由](../advanced/cookbook/resolving-routes.md)
128+
129+
## resolveRoutePath
130+
131+
- 详情:
132+
133+
解析给定链接对应的路由路径
134+
135+
- 参考:
136+
- [深入 > Cookbook > 解析路由](../advanced/cookbook/resolving-routes.md)
137+
109138
### withBase
110139

111140
- 详情:

0 commit comments

Comments
 (0)