Skip to content

Commit 6c0a4c6

Browse files
committed
feat: 新增 asVuePlugin 函数用于代替 createVueRouterPlugin 的作用
- 新增 `asVuePlugin` 函数,用于将 RouterPlugin 转换为 Vue 插件 - 标记 `createVueRouterPlugin` 为弃用 API,并推荐使用 `asVuePlugin` 代替 - 更新了 README 和相关文档,明确了两种插件模式的使用方法
1 parent 7bd9f68 commit 6c0a4c6

File tree

6 files changed

+96
-74
lines changed

6 files changed

+96
-74
lines changed

README.md

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ A lightweight solution providing standardized plugin system for Vue Router.
88

99
## 🌟 Core Features
1010

11-
| Feature | Description |
12-
| -------------------------------------- | ------------------------------------------------------------------------------ |
13-
| 🧱 **Standardized Plugin Interface** | Unified plugin development specification with auto-registration/uninstallation |
14-
| 🔁 **Reactive Side-effect Management** | Automatic tracking/cleanup of plugin's reactive side-effects |
15-
| ⚖️ **Dual-mode Compatibility** | Support both Vue Router plugin system and Vue plugin system |
11+
| Feature | Description |
12+
| -------------------------------------- | -------------------------------------------------------------------------------- |
13+
| 🧱 **Standardized Plugin Interface** | Unified plugin development specification with auto-registration/uninstallation |
14+
| 🔁 **Reactive Side-effect Management** | Automatic tracking/cleanup of plugin's reactive side-effects |
15+
| ⚖️ **Dual-mode Compatibility** | Supports both Vue Router plugin system and Vue plugin system compatibility modes |
1616

1717
---
1818

@@ -26,7 +26,7 @@ npm install vue-router-plugin-system
2626

2727
## 🚀 Getting Started
2828

29-
### Mode 1: Vue Router Plugin (Recommended)
29+
### Mode 1: Vue Router Plugin Mode (Recommended)
3030

3131
**1. Plugin Development**
3232

@@ -70,37 +70,39 @@ const router = createRouter({
7070
router.isNavigating.value
7171
```
7272

73-
### Mode 2: Vue Plugin (Compatibility)
73+
### Mode 2: Vue Plugin Mode (Compatibility)
7474

75-
**1. Create Vue Plugin**
75+
**1. Plugin Development**
7676

7777
```ts
78-
import { createVueRouterPlugin } from 'vue-router-plugin-system'
78+
import type { RouterPlugin } from 'vue-router-plugin-system'
79+
import { ref } from 'vue'
7980

80-
export const NavigationStatePlugin = createVueRouterPlugin((ctx) => {
81-
// Same implementation logic as above
82-
})
81+
export const NavigationStatePlugin: RouterPlugin = (ctx) => {
82+
// Implementation logic remains the same
83+
}
8384
```
8485

8586
**2. Application Integration**
8687

8788
```ts
8889
// main.ts
90+
import { asVuePlugin } from 'vue-router-plugin-system'
91+
8992
const app = createApp(App)
9093
app.use(router) // Mount router first
91-
app.use(NavigationStatePlugin) // Register plugin later
94+
app.use(asVuePlugin(NavigationStatePlugin)) // Register plugin later
9295
```
9396

9497
---
9598

9699
## ⚠️ Mode Comparison
97100

98-
| Feature | Vue Router Plugin | Vue Plugin |
99-
| ------------------------- | -------------------------------- | ------------------------------- |
100-
| Initialization Order | Prior to app logic | Dependent on client usage order |
101-
| Navigation Guard Priority | Higher | Dependent on registration order |
102-
| Reactive Management | Automatic cleanup | Automatic cleanup |
103-
| Dependency Requirement | Requires this lib's createRouter | No dependency required |
101+
| Feature | Vue Router Plugin Mode | Vue Plugin Mode |
102+
| -------------------- | -------------------------- | ----------------------------- |
103+
| Initialization Order | Prioritized over app logic | Depends on client usage order |
104+
| Guard Priority | Higher priority | Depends on registration order |
105+
| Reactive Management | Auto-cleanup | Auto-cleanup |
104106

105107
---
106108

@@ -113,11 +115,11 @@ interface RouterPluginContext {
113115
*/
114116
router: Router
115117
/**
116-
* Execute callback with vue app instance
118+
* Execute callback with Vue app instance
117119
*/
118120
runWithApp: (handler: RouterPluginRunWithAppHandler) => void
119121
/**
120-
* Register uninstall callback (can be called multiple times)
122+
* Register uninstallation callback (supports multiple calls)
121123
*/
122124
onUninstall: (handler: RouterPluginUninstallHandler) => void
123125
}
@@ -128,24 +130,17 @@ interface RouterPlugin {
128130
*/
129131
(ctx: RouterPluginContext): void
130132
}
131-
132-
interface VueRouterPlugin extends RouterPlugin {
133-
/**
134-
* Vue plugin installation function
135-
*/
136-
install: (app: App) => void
137-
}
138133
```
139134

140135
---
141136

142-
## 🤝 Contribution Guidelines
137+
## 🤝 Contribution Guide
143138

144-
Contributions welcome! Please ensure:
139+
Contributions are welcome! Please ensure:
145140

146-
1. Pass all unit tests
147-
2. Maintain TypeScript type integrity
148-
3. Add necessary documentation
141+
1. All unit tests pass
142+
2. TypeScript type integrity maintained
143+
3. Necessary documentation added
149144

150145
## License
151146

README.zh_CN.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,37 @@ router.isNavigating.value
7272

7373
### 模式二:Vue 插件模式(兼容方案)
7474

75-
**1. 创建 Vue 插件**
75+
**1. 开发插件**
7676

7777
```ts
78-
import { createVueRouterPlugin } from 'vue-router-plugin-system'
78+
import type { RouterPlugin } from 'vue-router-plugin-system'
79+
import { ref } from 'vue'
7980

80-
export const NavigationStatePlugin = createVueRouterPlugin((ctx) => {
81+
export const NavigationStatePlugin: RouterPlugin = (ctx) => {
8182
// 插件实现逻辑同上
82-
})
83+
}
8384
```
8485

8586
**2. 应用集成**
8687

8788
```ts
8889
// main.ts
90+
import { asVuePlugin } from 'vue-router-plugin-system'
91+
8992
const app = createApp(App)
9093
app.use(router) // 先挂载路由
91-
app.use(NavigationStatePlugin) // 后注册插件
94+
app.use(asVuePlugin(NavigationStatePlugin)) // 后注册插件
9295
```
9396

9497
---
9598

9699
## ⚠️ 模式对比
97100

98-
| 特性 | Vue Router 插件模式 | Vue 插件模式 |
99-
| -------------- | --------------------- | ------------------ |
100-
| 初始化顺序 | 优先于应用逻辑 | 依赖客户端使用顺序 |
101-
| 导航守卫优先级 | 更高 | 依赖注册顺序 |
102-
| 响应式管理 | 自动清理 | 自动清理 |
103-
| 依赖要求 | 需要本库 createRouter | 无需本库 |
101+
| 特性 | Vue Router 插件模式 | Vue 插件模式 |
102+
| -------------- | ------------------- | ------------------ |
103+
| 初始化顺序 | 优先于应用逻辑 | 依赖客户端使用顺序 |
104+
| 导航守卫优先级 | 更高 | 依赖注册顺序 |
105+
| 响应式管理 | 自动清理 | 自动清理 |
104106

105107
---
106108

@@ -128,13 +130,6 @@ interface RouterPlugin {
128130
*/
129131
(ctx: RouterPluginContext): void
130132
}
131-
132-
interface VueRouterPlugin extends RouterPlugin {
133-
/**
134-
* Vue 插件安装函数
135-
*/
136-
install: (app: App) => void
137-
}
138133
```
139134

140135
---

src/as-vue-plugin.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { App, ObjectPlugin } from 'vue'
2+
import type { RouterPlugin } from './plugin'
3+
import { prepareInstall } from './prepare-install'
4+
import { setupPlugin } from './setup-plugin'
5+
6+
/**
7+
* Convert a {@link RouterPlugin} to a {@link ObjectPlugin VuePlugin}.
8+
*
9+
* @description Wraps the plugin with an `install` method to adapt it to Vue's plugin registration mechanism.
10+
*
11+
* @param plugin {@link RouterPlugin}
12+
*
13+
* @returns Returns a {@link ObjectPlugin VuePlugin}.
14+
*
15+
* @example
16+
* ```ts
17+
* const SomePlugin: RouterPlugin = (ctx) => {
18+
* // plugin implementation
19+
* }
20+
*
21+
* // must be installed after the router
22+
* app.use(router).use(asVuePlugin(SomePlugin))
23+
* ```
24+
*/
25+
export function asVuePlugin(plugin: RouterPlugin): RouterPlugin & ObjectPlugin {
26+
return Object.assign(plugin, {
27+
install(app: App) {
28+
const router = app.config.globalProperties.$router
29+
if (!router) {
30+
throw new Error(
31+
'[vue-router-plugin-system] Please install vue-router first.',
32+
)
33+
}
34+
35+
prepareInstall({ app, router })
36+
setupPlugin({ router, plugin })
37+
},
38+
})
39+
}

src/create-vue-router-plugin.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { App } from 'vue'
22
import type { RouterPlugin } from './plugin'
3-
import { prepareInstall } from './prepare-install'
4-
import { setupPlugin } from './setup-plugin'
3+
import { asVuePlugin } from './as-vue-plugin'
54

5+
/**
6+
* @deprecated
7+
*/
68
export interface VueRouterPlugin extends RouterPlugin {
79
/**
810
* Install the plugin.
@@ -13,6 +15,9 @@ export interface VueRouterPlugin extends RouterPlugin {
1315

1416
/**
1517
* Create a {@link VueRouterPlugin} from a {@link RouterPlugin}.
18+
*
19+
* @deprecated Please use {@link asVuePlugin} instead.
20+
*
1621
* @param plugin {@link RouterPlugin}
1722
* @returns Returns a {@link VueRouterPlugin}
1823
* @example
@@ -25,18 +30,5 @@ export interface VueRouterPlugin extends RouterPlugin {
2530
* app.use(router).use(SomePlugin)
2631
* ```
2732
*/
28-
export function createVueRouterPlugin(plugin: RouterPlugin): VueRouterPlugin {
29-
return Object.assign(plugin, {
30-
install(app: App) {
31-
const router = app.config.globalProperties.$router
32-
if (!router) {
33-
throw new Error(
34-
'[vue-router-plugin-system] Please install vue-router first.',
35-
)
36-
}
37-
38-
prepareInstall({ app, router })
39-
setupPlugin({ router, plugin })
40-
},
41-
})
42-
}
33+
export const createVueRouterPlugin: (plugin: RouterPlugin) => VueRouterPlugin =
34+
asVuePlugin

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './as-vue-plugin'
12
export * from './create-router'
23
export * from './create-vue-router-plugin'
34
export * from './plugin'

tests/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RouterPlugin } from '../src/plugin'
22
import { describe, expect, it, vi } from 'vitest'
33
import { computed, createApp, ref, watch } from 'vue'
44
import { createMemoryHistory } from 'vue-router'
5-
import { createRouter, createVueRouterPlugin } from '../src/index'
5+
import { asVuePlugin, createRouter } from '../src/index'
66

77
describe.concurrent('vue Router Plugin System', () => {
88
// 测试插件基础功能
@@ -107,23 +107,23 @@ describe.concurrent('vue Router Plugin System', () => {
107107
expect(logSpy).toHaveBeenCalledTimes(2)
108108
})
109109

110-
// 测试从 vue app 进行安装并且功能正常
111-
it('should install from vue app', () => {
110+
// 测试 asVuePlugin
111+
it('test asVuePlugin', () => {
112112
const counter = ref(0)
113113
const logSpy = vi.fn()
114-
const mockPlugin = createVueRouterPlugin(() => {
114+
const mockPlugin: RouterPlugin = () => {
115115
const doubled = computed(() => counter.value * 2)
116116
watch(doubled, logSpy, { immediate: true, flush: 'sync' })
117117
counter.value = 1
118-
})
118+
}
119119

120120
const router = createRouter({
121121
history: createMemoryHistory(),
122122
routes: [],
123123
})
124124

125125
const app = createApp(() => null)
126-
app.use(router).use(mockPlugin)
126+
app.use(router).use(asVuePlugin(mockPlugin))
127127
app.unmount()
128128

129129
expect(logSpy).toHaveBeenCalledTimes(2)

0 commit comments

Comments
 (0)