Skip to content

Commit b956767

Browse files
committed
docs(README): 更新中文和英文文档
- 重新组织文档结构,增加核心功能介绍 - 添加动态路由缓存和嵌套路由渲染的详细说明 - 更新示例代码和API文档 - 增加升级指南和赞助信息
1 parent d1178de commit b956767

File tree

10 files changed

+316
-99
lines changed

10 files changed

+316
-99
lines changed

.github/workflows/deployPlay.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ permissions:
1111
contents: write
1212

1313
concurrency:
14-
group: 'pages'
14+
group: pages
1515
cancel-in-progress: true
1616

1717
jobs:

.vscode/extensions.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"recommendations": [
3-
"Vue.volar",
4-
"dbaeumer.vscode-eslint",
5-
"esbenp.prettier-vscode",
6-
]
2+
"recommendations": ["Vue.volar", "dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
73
}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@
121121
"jsonc",
122122
"yaml",
123123
"toml"
124-
],
124+
]
125125
}

README.md

Lines changed: 150 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
# vue-router-better-view
22

3-
Enhances the [RouterView & KeepAlive](https://router.vuejs.org/guide/advanced/router-view-slot.html#KeepAlive-Transition) functionality of [Vue Router](https://router.vuejs.org/).
3+
An extension for [Vue Router](https://router.vuejs.org/)'s [RouterView](https://router.vuejs.org/guide/advanced/router-view-slot.html#RouterView-slot) with enhanced features.
44

5-
[中文文档](https://github.com/l246804/vue-router-better-view/blob/dev/README.zh-CN.md)
5+
**Key Features**
66

7-
[Online example](https://l246804.github.io/vue-router-better-view/)
7+
- [x] Enhanced [RouterView & KeepAlive](https://router.vuejs.org/guide/advanced/router-view-slot.html#KeepAlive-Transition) functionality with support for dynamic parameter route caching
8+
- [x] Precise rendering (ignore parent routes with components) for intuitive nested route handling
9+
10+
[中文文档](./README.zh-CN.md) | [Online Demo](https://l246804.github.io/vue-router-better-view/)
11+
12+
---
813

914
## Background
1015

11-
The current [RouterView](file:///home/leihaohao/workspaces/own/vue-router-better-view/node_modules/.pnpm/[email protected][email protected][email protected]_/node_modules/vue-router/dist/vue-router.d.ts#L1603-L1613) component in Vue Router has the following limitations when used with the [KeepAlive](https://cn.vuejs.org/api/built-in-components.html#keepalive) component:
16+
Current issues with Vue Router:
17+
18+
### 1. **Dynamic Route Caching Problems**
19+
20+
- **Issue 1**: When using `KeepAlive`, cannot precisely control component instance caching for dynamic routes (e.g., `/user/:id`)
21+
- **Issue 2**: Route component `name` must match route configuration's `name` attribute for proper caching
1222

13-
1. Unable to cache different parameters of the same component instance based on [dynamic route matching](https://router.vuejs.org/guide/essentials/dynamic-matching.html).
14-
2. Route components must have distinct [name](file:///home/leihaohao/workspaces/own/vue-router-better-view/node_modules/.pnpm/[email protected][email protected][email protected]_/node_modules/vue-router/dist/vue-router.d.ts#L244-L244) attributes; otherwise, components with the same name will cause caching issues.
23+
### 2. **Nested Route Rendering Issues**
1524

16-
This plugin provides a simple and efficient solution to the above problems.
25+
- **Issue 1**: Official nested routes require either nested `RouterView` or flat route registration, leading to:
26+
- **Nested RouterView approach**: Redundant rendering logic in sub-pages
27+
- **Flat registration**: Loses `route.matched` convenience and feels unintuitive
28+
29+
This plugin solves these issues through the `BetterRouterView` component with a more efficient solution.
30+
31+
---
1732

1833
## Installation
1934

20-
```sh
35+
```bash
2136
npm i vue-router-better-view
2237
```
2338

@@ -26,7 +41,7 @@ npm i vue-router-better-view
2641
```ts
2742
import { BetterRouterView } from 'vue-router-better-view'
2843

29-
// Globally register the BetterRouterView component
44+
// Globally register BetterRouterView component
3045
app.use(BetterRouterView)
3146
```
3247

@@ -48,21 +63,19 @@ app.use(BetterRouterView)
4863
</template>
4964
```
5065

51-
## Component Props
66+
---
5267

53-
```ts
54-
export interface BetterRouterViewProps extends RouterViewProps {
55-
/**
56-
* Retrieves the view component key for the current route.
57-
* If not set or returns a falsy value, behaves like the standard `RouterView` component.
58-
* @param route The current route
59-
* @returns The view component key
60-
*/
61-
resolveViewKey?: ResolveViewKey
62-
}
63-
```
68+
## Core Features
69+
70+
### 1. **Dynamic Route Caching Optimization**
71+
72+
#### Problem
73+
74+
By default, `/user/1` and `/user/2` are treated as same component, preventing fine-grained caching control.
6475

65-
## Example
76+
#### Solution
77+
78+
Custom cache identifier via `resolveViewKey`:
6679

6780
```html
6881
<script
@@ -72,12 +85,9 @@ export interface BetterRouterViewProps extends RouterViewProps {
7285
import { BetterRouterView, type ResolveViewKey } from 'vue-router-better-view'
7386
7487
const resolveViewKey: ResolveViewKey = (route) => {
75-
// If route.meta.singleton is true, use the route's path as the view component key
76-
if (route.meta.singleton) {
77-
return route.path
78-
}
79-
80-
// Use the route's fullPath as the view component key
88+
// Cache by path if marked as singleton
89+
if (route.meta.singleton) return route.path
90+
// Cache by fullPath (including params) otherwise
8191
return route.fullPath
8292
}
8393
</script>
@@ -88,7 +98,7 @@ export interface BetterRouterViewProps extends RouterViewProps {
8898
v-slot="{ Component }"
8999
:resolve-view-key="resolveViewKey"
90100
>
91-
<!-- include/exclude can be used based on the return value of resolveViewKey -->
101+
<!-- Include/exclude based on resolveViewKey value -->
92102
<keep-alive>
93103
<component :is="Component" />
94104
</keep-alive>
@@ -97,22 +107,121 @@ export interface BetterRouterViewProps extends RouterViewProps {
97107
</template>
98108
```
99109

100-
### Template Refs
110+
---
111+
112+
### 2. **Precise Nested Route Rendering**
113+
114+
#### Use Case
115+
116+
Directly render `<UserDetail />` for `/system/user/detail` instead of parent component `<User />`
117+
118+
#### Configuration Example
119+
120+
```ts
121+
// src/router/index.ts
122+
const routes: RouteRecordRaw[] = [
123+
{
124+
path: '/system',
125+
component: Layout, // Layout component requiring precise rendering
126+
children: [
127+
{
128+
path: 'user',
129+
component: UserLayout,
130+
children: [
131+
{
132+
path: 'detail/:id?',
133+
component: UserDetail,
134+
},
135+
],
136+
},
137+
],
138+
},
139+
]
140+
```
141+
142+
#### Implementation Methods
143+
144+
- **Using `<BetterRouterView />`**:
145+
146+
```html
147+
<script
148+
setup
149+
lang="ts"
150+
>
151+
import { BetterRouterView } from 'vue-router-better-view'
152+
</script>
153+
154+
<template>
155+
<main>
156+
<!-- Enable precise rendering with 'exact' attribute -->
157+
<better-router-view exact />
158+
</main>
159+
</template>
160+
```
161+
162+
- **Using `useExactView` Function**:
101163

102-
```diff
103-
<script setup lang="ts">
104-
+import { ref } from 'vue'
164+
> Must be called in the component containing `<RouterView />`
105165
106-
+const mainContent = ref<any>()
107-
+const resolveMainContent = () => {
108-
+ // Prefer using the inner property to get the reference of the original component;
109-
+ // if it doesn't exist, fall back to mainContent.value.
110-
+ return mainContent.value?.inner || mainContent.value
111-
+}
166+
```html
167+
<script
168+
setup
169+
lang="ts"
170+
>
171+
import { useExactView } from 'vue-router-better-view'
172+
173+
useExactView({ exact: true })
112174
</script>
113175

114176
<template>
115-
- <component :is="Component" />
116-
+ <component :is="Component" ref="mainContent" />
177+
<main>
178+
<!-- Can use native RouterView component -->
179+
<router-view />
180+
</main>
117181
</template>
118182
```
183+
184+
---
185+
186+
## API
187+
188+
### BetterRouterView
189+
190+
```ts
191+
interface BetterRouterViewProps extends RouterViewProps {
192+
/**
193+
* Gets view component identifier for current route
194+
* - When unset or returns falsy value, behaves like standard RouterView
195+
* @param route Current route
196+
* @returns View component identifier
197+
*/
198+
resolveViewKey?: ResolveViewKey
199+
/**
200+
* Whether to enable precise route matching
201+
* - boolean: `true` enables precise matching but disallows nested RouterView
202+
* - number: Enables precise matching with nested RouterView support
203+
* - null: Default, restores standard RouterView behavior
204+
* @default null
205+
*/
206+
exact?: boolean | number | null
207+
}
208+
```
209+
210+
---
211+
212+
## Migration Guide
213+
214+
### Upgrading to v1.0.0
215+
216+
- Fully compatible with standard `<RouterView>` usage
217+
- Ensure `exact` attribute is enabled in nested route configurations
218+
219+
---
220+
221+
## Sponsor
222+
223+
Your support fuels continuous improvement! If this project helps you, consider buying me a juice 🍹:
224+
225+
| WeChat | Alipay |
226+
| --------------------------------------- | ---------------------------------------- |
227+
| <img src="./public/wx.jpg" width="200"> | <img src="./public/zfb.jpg" width="200"> |

0 commit comments

Comments
 (0)