@deppon/deppon-router 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +660 -0
- package/es/_virtual/_rollup-plugin-inject-process-env.js +11 -0
- package/es/composable.d.ts +36 -0
- package/es/composable.js +138 -0
- package/es/guards.d.ts +8 -0
- package/es/guards.js +122 -0
- package/es/index.d.ts +87 -0
- package/es/index.js +99 -0
- package/es/types.d.ts +4 -0
- package/es/utils.d.ts +142 -0
- package/es/utils.js +558 -0
- package/es/vue.d.ts +16 -0
- package/es/vue.js +42 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Deppon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
# `@deppon/deppon-router`
|
|
2
|
+
|
|
3
|
+
德邦前端 Vue Router 4 封装包
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @deppon/deppon-router
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**依赖说明**:
|
|
12
|
+
|
|
13
|
+
- `vue-router` 已声明为 `peerDependencies`,如果宿主项目已安装 `vue-router@^4.0.0`,将优先使用宿主项目的版本,避免版本冲突
|
|
14
|
+
- 如果宿主项目未安装 `vue-router`,会自动安装(因为也在 `dependencies` 中)
|
|
15
|
+
- 建议宿主项目显式安装 `vue-router@^4.0.0` 以确保版本一致性
|
|
16
|
+
- viewTab 相关功能已内置在 `@deppon/deppon-router` 中,不再需要 `@deppon/deppon-utils` 依赖
|
|
17
|
+
|
|
18
|
+
## 功能特性
|
|
19
|
+
|
|
20
|
+
- ✅ 基于 Vue Router 4 封装
|
|
21
|
+
- ✅ 支持 Vue 3 插件安装方式
|
|
22
|
+
- ✅ 提供 Composition API (useRouter, useRoute)
|
|
23
|
+
- ✅ 支持路由守卫配置
|
|
24
|
+
- ✅ 自动路由追踪(与 deppon-log 集成)
|
|
25
|
+
- ✅ 提供路由工具函数
|
|
26
|
+
- ✅ 自动设置页面标题
|
|
27
|
+
- ✅ 安全的路由跳转(错误处理)
|
|
28
|
+
- ✅ 智能路由跳转(在 iframe 环境中自动使用 viewTab,否则使用 router.push)
|
|
29
|
+
|
|
30
|
+
## 基础使用
|
|
31
|
+
|
|
32
|
+
### JavaScript/TypeScript 项目
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import { createDepponRouter } from '@deppon/deppon-router';
|
|
36
|
+
|
|
37
|
+
// 创建路由实例
|
|
38
|
+
const router = createDepponRouter({
|
|
39
|
+
routes: [
|
|
40
|
+
{
|
|
41
|
+
path: '/',
|
|
42
|
+
name: 'Home',
|
|
43
|
+
component: () => import('./views/Home.vue'),
|
|
44
|
+
meta: {
|
|
45
|
+
title: '首页',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
path: '/about',
|
|
50
|
+
name: 'About',
|
|
51
|
+
component: () => import('./views/About.vue'),
|
|
52
|
+
meta: {
|
|
53
|
+
title: '关于',
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
routerOptions: {
|
|
58
|
+
history: 'web', // 'web' | 'hash' | 'memory',默认为 'web'
|
|
59
|
+
base: '/',
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export default router;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Vue 3 使用
|
|
67
|
+
|
|
68
|
+
### 1. 安装插件
|
|
69
|
+
|
|
70
|
+
在 Vue 应用中安装插件:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
import { createApp } from 'vue';
|
|
74
|
+
import { VuePlugin } from '@deppon/deppon-router';
|
|
75
|
+
import App from './App.vue';
|
|
76
|
+
|
|
77
|
+
const app = createApp(App);
|
|
78
|
+
|
|
79
|
+
// 安装插件
|
|
80
|
+
app.use(VuePlugin, {
|
|
81
|
+
routes: [
|
|
82
|
+
{
|
|
83
|
+
path: '/',
|
|
84
|
+
name: 'Home',
|
|
85
|
+
component: () => import('./views/Home.vue'),
|
|
86
|
+
meta: {
|
|
87
|
+
title: '首页',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
routerOptions: {
|
|
92
|
+
history: 'web', // 'web' | 'hash' | 'memory'
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
app.mount('#app');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 2. 配置路由守卫
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
import { createApp } from 'vue';
|
|
103
|
+
import { VuePlugin } from '@deppon/deppon-router';
|
|
104
|
+
import App from './App.vue';
|
|
105
|
+
|
|
106
|
+
const app = createApp(App);
|
|
107
|
+
|
|
108
|
+
app.use(VuePlugin, {
|
|
109
|
+
routes: [
|
|
110
|
+
// ... 路由配置
|
|
111
|
+
],
|
|
112
|
+
guards: {
|
|
113
|
+
// 全局前置守卫
|
|
114
|
+
beforeEach(to, from, next) {
|
|
115
|
+
// 权限检查
|
|
116
|
+
if (to.meta.requiresAuth && !isAuthenticated()) {
|
|
117
|
+
next('/login');
|
|
118
|
+
} else {
|
|
119
|
+
next();
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
// 全局后置守卫
|
|
123
|
+
afterEach(to, from) {
|
|
124
|
+
// 路由变化后的处理
|
|
125
|
+
console.log('路由变化:', to.path);
|
|
126
|
+
},
|
|
127
|
+
// 全局解析守卫
|
|
128
|
+
beforeResolve(to, from, next) {
|
|
129
|
+
// 解析前的处理
|
|
130
|
+
next();
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
app.mount('#app');
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 3. 与 deppon-log 集成
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
import { createApp } from 'vue';
|
|
142
|
+
import { VuePlugin } from '@deppon/deppon-router';
|
|
143
|
+
import { VuePlugin as LogPlugin } from '@deppon/deppon-log';
|
|
144
|
+
import App from './App.vue';
|
|
145
|
+
|
|
146
|
+
const app = createApp(App);
|
|
147
|
+
|
|
148
|
+
// 先创建路由实例
|
|
149
|
+
const router = createDepponRouter({
|
|
150
|
+
routes: [
|
|
151
|
+
// ... 路由配置
|
|
152
|
+
],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// 安装路由插件(传入 log 实例以启用自动路由追踪)
|
|
156
|
+
app.use(VuePlugin, {
|
|
157
|
+
router,
|
|
158
|
+
log: logInstance, // deppon-log 实例
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// 或者使用插件方式
|
|
162
|
+
app.use(VuePlugin, {
|
|
163
|
+
routes: [
|
|
164
|
+
// ... 路由配置
|
|
165
|
+
],
|
|
166
|
+
log: logInstance,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
app.mount('#app');
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 4. 在 Composition API 中使用
|
|
173
|
+
|
|
174
|
+
```vue
|
|
175
|
+
<script setup>
|
|
176
|
+
import { useRouter, useRoute } from '@deppon/deppon-router';
|
|
177
|
+
import { ref } from 'vue';
|
|
178
|
+
|
|
179
|
+
const router = useRouter();
|
|
180
|
+
const route = useRoute();
|
|
181
|
+
|
|
182
|
+
// 获取当前路由信息
|
|
183
|
+
console.log(route.path);
|
|
184
|
+
console.log(route.params);
|
|
185
|
+
console.log(route.query);
|
|
186
|
+
|
|
187
|
+
// 路由跳转
|
|
188
|
+
const goToHome = () => {
|
|
189
|
+
router.push('/home');
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// 使用工具方法
|
|
193
|
+
const goToAbout = () => {
|
|
194
|
+
router.pushByName('About', {}, {});
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const goBack = () => {
|
|
198
|
+
router.goBack();
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// 使用智能获取路由参数(自动适配 iframe 环境)
|
|
202
|
+
const route = useRoute();
|
|
203
|
+
const sourceType = route.value.getQueryByRoute('sourceType');
|
|
204
|
+
const allParams = route.value.getQueryByRoute();
|
|
205
|
+
|
|
206
|
+
// 使用智能路由跳转(在 iframe 环境中自动使用 viewTab)
|
|
207
|
+
const goToDetail = () => {
|
|
208
|
+
router.smartPush({
|
|
209
|
+
path: '/preferInfo',
|
|
210
|
+
query: { sourceType: 'insert' },
|
|
211
|
+
params: { custNumber: '701265308' },
|
|
212
|
+
_viewTab: {
|
|
213
|
+
title: 'CMC-产品折扣新增',
|
|
214
|
+
uumsFunction: {
|
|
215
|
+
functionCode: 'CMC_FUNCTION_00002',
|
|
216
|
+
sourceSystem: 'CMC',
|
|
217
|
+
},
|
|
218
|
+
closeCurrentTab: true,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
</script>
|
|
223
|
+
|
|
224
|
+
<template>
|
|
225
|
+
<div>
|
|
226
|
+
<button @click="goToHome">去首页</button>
|
|
227
|
+
<button @click="goToAbout">去关于页</button>
|
|
228
|
+
<button @click="goBack">返回</button>
|
|
229
|
+
</div>
|
|
230
|
+
</template>
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 5. 在 Options API 中使用
|
|
234
|
+
|
|
235
|
+
```vue
|
|
236
|
+
<script>
|
|
237
|
+
export default {
|
|
238
|
+
methods: {
|
|
239
|
+
goToHome() {
|
|
240
|
+
// 使用 this.$router 访问
|
|
241
|
+
this.$router.push('/home');
|
|
242
|
+
},
|
|
243
|
+
goToAbout() {
|
|
244
|
+
// 使用工具方法
|
|
245
|
+
this.$router.pushByName('About');
|
|
246
|
+
},
|
|
247
|
+
goBack() {
|
|
248
|
+
this.$router.goBack();
|
|
249
|
+
},
|
|
250
|
+
getParams() {
|
|
251
|
+
// 使用智能获取路由参数(自动适配 iframe 环境)
|
|
252
|
+
const allParams = this.$router.getQueryByRoute();
|
|
253
|
+
const sourceType = this.$router.getQueryByRoute('sourceType');
|
|
254
|
+
console.log('所有参数:', allParams);
|
|
255
|
+
console.log('sourceType:', sourceType);
|
|
256
|
+
},
|
|
257
|
+
goToDetail() {
|
|
258
|
+
// 使用智能路由跳转(在 iframe 环境中自动使用 viewTab)
|
|
259
|
+
this.$router.smartPush({
|
|
260
|
+
path: '/preferInfo',
|
|
261
|
+
query: { sourceType: 'insert' },
|
|
262
|
+
_viewTab: {
|
|
263
|
+
title: 'CMC-产品折扣新增',
|
|
264
|
+
closeCurrentTab: true,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
mounted() {
|
|
270
|
+
// 访问当前路由
|
|
271
|
+
console.log(this.$route.path);
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
</script>
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## API
|
|
278
|
+
|
|
279
|
+
### createDepponRouter(options)
|
|
280
|
+
|
|
281
|
+
创建增强的 Vue Router 实例。
|
|
282
|
+
|
|
283
|
+
#### 参数
|
|
284
|
+
|
|
285
|
+
- `options.routes` - 路由配置数组(必填)
|
|
286
|
+
- `options.routerOptions` - Vue Router 原始配置选项(可选)
|
|
287
|
+
- `history` - 历史模式('web' | 'hash' | 'memory'),默认为 'web'
|
|
288
|
+
- `base` - 应用的基础路径
|
|
289
|
+
- `scrollBehavior` - 滚动行为函数
|
|
290
|
+
- 其他 Vue Router 4 支持的选项
|
|
291
|
+
- `options.guards` - 路由守卫配置(可选)
|
|
292
|
+
- `beforeEach` - 全局前置守卫
|
|
293
|
+
- `afterEach` - 全局后置守卫
|
|
294
|
+
- `beforeResolve` - 全局解析守卫
|
|
295
|
+
- `options.log` - 日志实例(可选,用于路由追踪)
|
|
296
|
+
- `options.onRouteChange` - 路由变化回调(可选)
|
|
297
|
+
|
|
298
|
+
#### 返回值
|
|
299
|
+
|
|
300
|
+
增强的 Vue Router 实例,包含以下额外方法:
|
|
301
|
+
|
|
302
|
+
- `safePush(location, onComplete, onAbort)` - 安全的路由跳转
|
|
303
|
+
- `safeReplace(location, onComplete, onAbort)` - 安全的路由替换
|
|
304
|
+
- `pushByName(name, params, query)` - 根据路由名称跳转
|
|
305
|
+
- `pushByPath(path, query)` - 根据路由路径跳转
|
|
306
|
+
- `goBack(delta)` - 返回上一页
|
|
307
|
+
- `hasRoute(name)` - 检查路由是否存在(包括动态添加的路由)
|
|
308
|
+
- `getRouteConfig(name)` - 获取路由配置(包括动态添加的路由)
|
|
309
|
+
- `smartPush(location, onComplete, onAbort)` - 智能路由跳转(在 iframe 环境中自动使用 viewTab,否则使用 router.push)
|
|
310
|
+
- `getQueryByRoute(key)` - 智能获取路由参数(在 iframe 环境中优先从父级获取,否则从当前路由 query 获取)
|
|
311
|
+
|
|
312
|
+
### Composition API
|
|
313
|
+
|
|
314
|
+
#### useRouter()
|
|
315
|
+
|
|
316
|
+
获取 router 实例。
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
import { useRouter } from '@deppon/deppon-router';
|
|
320
|
+
|
|
321
|
+
const router = useRouter();
|
|
322
|
+
router.push('/home');
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
#### useRoute()
|
|
326
|
+
|
|
327
|
+
获取当前路由对象。
|
|
328
|
+
|
|
329
|
+
```javascript
|
|
330
|
+
import { useRoute } from '@deppon/deppon-router';
|
|
331
|
+
|
|
332
|
+
const route = useRoute();
|
|
333
|
+
console.log(route.value.path);
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## 路由配置示例
|
|
337
|
+
|
|
338
|
+
```javascript
|
|
339
|
+
const routes = [
|
|
340
|
+
{
|
|
341
|
+
path: '/',
|
|
342
|
+
name: 'Home',
|
|
343
|
+
component: () => import('./views/Home.vue'),
|
|
344
|
+
meta: {
|
|
345
|
+
title: '首页',
|
|
346
|
+
requiresAuth: false,
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
path: '/user',
|
|
351
|
+
name: 'User',
|
|
352
|
+
component: () => import('./views/User.vue'),
|
|
353
|
+
meta: {
|
|
354
|
+
title: '用户中心',
|
|
355
|
+
requiresAuth: true,
|
|
356
|
+
},
|
|
357
|
+
children: [
|
|
358
|
+
{
|
|
359
|
+
path: 'profile',
|
|
360
|
+
name: 'UserProfile',
|
|
361
|
+
component: () => import('./views/UserProfile.vue'),
|
|
362
|
+
meta: {
|
|
363
|
+
title: '个人资料',
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
],
|
|
367
|
+
},
|
|
368
|
+
];
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## 路由守卫示例
|
|
372
|
+
|
|
373
|
+
### 权限控制
|
|
374
|
+
|
|
375
|
+
```javascript
|
|
376
|
+
app.use(VuePlugin, {
|
|
377
|
+
routes: [
|
|
378
|
+
// ... 路由配置
|
|
379
|
+
],
|
|
380
|
+
guards: {
|
|
381
|
+
beforeEach(to, from, next) {
|
|
382
|
+
// 检查是否需要登录
|
|
383
|
+
if (to.meta.requiresAuth && !isAuthenticated()) {
|
|
384
|
+
next({
|
|
385
|
+
path: '/login',
|
|
386
|
+
query: { redirect: to.fullPath },
|
|
387
|
+
});
|
|
388
|
+
} else {
|
|
389
|
+
next();
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### 页面标题管理
|
|
397
|
+
|
|
398
|
+
路由配置中的 `meta.title` 会自动设置为页面标题:
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
{
|
|
402
|
+
path: '/about',
|
|
403
|
+
name: 'About',
|
|
404
|
+
component: () => import('./views/About.vue'),
|
|
405
|
+
meta: {
|
|
406
|
+
title: '关于我们', // 会自动设置为 document.title
|
|
407
|
+
},
|
|
408
|
+
}
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
## 与 deppon-log 集成
|
|
412
|
+
|
|
413
|
+
当传入 `log` 实例时,会自动追踪路由变化:
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
import { VuePlugin as LogPlugin } from '@deppon/deppon-log';
|
|
417
|
+
import { VuePlugin as RouterPlugin } from '@deppon/deppon-router';
|
|
418
|
+
|
|
419
|
+
// 初始化日志
|
|
420
|
+
const logInstance = LogPlugin.init(/* ... */);
|
|
421
|
+
|
|
422
|
+
// 安装路由插件,传入 log 实例
|
|
423
|
+
app.use(RouterPlugin, {
|
|
424
|
+
routes: [
|
|
425
|
+
// ... 路由配置
|
|
426
|
+
],
|
|
427
|
+
log: logInstance, // 自动追踪路由变化
|
|
428
|
+
});
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
路由变化时会自动触发以下埋点事件:
|
|
432
|
+
|
|
433
|
+
- `$route_before` - 路由跳转前
|
|
434
|
+
- `$pageview` - 页面浏览
|
|
435
|
+
|
|
436
|
+
## 智能获取路由参数(getQueryByRoute)
|
|
437
|
+
|
|
438
|
+
`getQueryByRoute` 是一个智能获取路由参数的方法,它会自动检测当前环境并获取相应的参数:
|
|
439
|
+
|
|
440
|
+
- 在 iframe 环境中:优先从父级获取 params(通过 `getParentParams`),同时合并当前路由的 query 参数
|
|
441
|
+
- 在非 iframe 环境中:从当前路由的 query 参数获取
|
|
442
|
+
|
|
443
|
+
### 基本用法
|
|
444
|
+
|
|
445
|
+
```javascript
|
|
446
|
+
import { useRouter, useRoute } from '@deppon/deppon-router';
|
|
447
|
+
|
|
448
|
+
// 方式1:通过 router 实例使用
|
|
449
|
+
const router = useRouter();
|
|
450
|
+
const allParams = router.getQueryByRoute(); // 获取所有参数
|
|
451
|
+
const sourceType = router.getQueryByRoute('sourceType'); // 获取指定参数
|
|
452
|
+
|
|
453
|
+
// 方式2:通过 route 对象使用(推荐)
|
|
454
|
+
const route = useRoute();
|
|
455
|
+
const allParams = route.value.getQueryByRoute(); // 获取所有参数
|
|
456
|
+
const sourceType = route.value.getQueryByRoute('sourceType'); // 获取指定参数
|
|
457
|
+
|
|
458
|
+
// 方式3:在 Options API 中使用
|
|
459
|
+
this.$router.getQueryByRoute('sourceType');
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### 参数说明
|
|
463
|
+
|
|
464
|
+
- `key` (string, 可选): 参数键名
|
|
465
|
+
- 如果提供 `key`,返回对应的参数值(不存在返回 `null`)
|
|
466
|
+
- 如果不提供 `key`,返回所有参数对象
|
|
467
|
+
|
|
468
|
+
### 参数获取逻辑
|
|
469
|
+
|
|
470
|
+
1. **iframe 环境**:
|
|
471
|
+
|
|
472
|
+
- 优先从父级获取 params(`top.Ext.getCmp('mainAreaPanel').params`)
|
|
473
|
+
- 同时从当前路由 query 获取参数
|
|
474
|
+
- 合并两者:父级 params 作为基础,query 参数覆盖同名参数(query 优先级更高)
|
|
475
|
+
|
|
476
|
+
2. **非 iframe 环境**:
|
|
477
|
+
- 从当前路由 query 获取参数(`router.currentRoute.value.query`)
|
|
478
|
+
|
|
479
|
+
### 使用示例
|
|
480
|
+
|
|
481
|
+
```javascript
|
|
482
|
+
// 在 Composition API 中使用
|
|
483
|
+
import { useRoute } from '@deppon/deppon-router';
|
|
484
|
+
|
|
485
|
+
export default {
|
|
486
|
+
setup() {
|
|
487
|
+
const route = useRoute();
|
|
488
|
+
|
|
489
|
+
// 获取所有参数
|
|
490
|
+
const params = route.value.getQueryByRoute();
|
|
491
|
+
console.log('所有参数:', params);
|
|
492
|
+
|
|
493
|
+
// 获取指定参数
|
|
494
|
+
const sourceType = route.value.getQueryByRoute('sourceType');
|
|
495
|
+
const custNumber = route.value.getQueryByRoute('custNumber');
|
|
496
|
+
|
|
497
|
+
return {
|
|
498
|
+
sourceType,
|
|
499
|
+
custNumber,
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// 在 Options API 中使用
|
|
505
|
+
export default {
|
|
506
|
+
mounted() {
|
|
507
|
+
// 获取所有参数
|
|
508
|
+
const params = this.$router.getQueryByRoute();
|
|
509
|
+
console.log('所有参数:', params);
|
|
510
|
+
|
|
511
|
+
// 获取指定参数
|
|
512
|
+
const sourceType = this.$router.getQueryByRoute('sourceType');
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### 替换原有代码示例
|
|
518
|
+
|
|
519
|
+
**原代码(需要手动判断环境):**
|
|
520
|
+
|
|
521
|
+
```javascript
|
|
522
|
+
export function getQueryByRoute(key) {
|
|
523
|
+
let params;
|
|
524
|
+
if (top && top.viewTab) {
|
|
525
|
+
params =
|
|
526
|
+
(top.Ext && top.Ext.getCmp && top.Ext.getCmp('mainAreaPanel') && top.Ext.getCmp('mainAreaPanel').params) ||
|
|
527
|
+
{};
|
|
528
|
+
} else {
|
|
529
|
+
params = router.currentRoute.query;
|
|
530
|
+
}
|
|
531
|
+
return !key ? params : params ? params[key] : null;
|
|
532
|
+
}
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
**新代码(自动适配):**
|
|
536
|
+
|
|
537
|
+
```javascript
|
|
538
|
+
import { useRoute } from '@deppon/deppon-router';
|
|
539
|
+
|
|
540
|
+
const route = useRoute();
|
|
541
|
+
const sourceType = route.value.getQueryByRoute('sourceType');
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### 注意事项
|
|
545
|
+
|
|
546
|
+
1. `getQueryByRoute` 在 iframe 环境中会自动从父级获取参数,无需额外依赖
|
|
547
|
+
2. 在 iframe 环境中,父级 params 和路由 query 参数会被合并,query 参数优先级更高
|
|
548
|
+
3. 如果参数不存在,返回 `null`(当提供 key 时)或空对象 `{}`(当不提供 key 时)
|
|
549
|
+
|
|
550
|
+
## 智能路由跳转(smartPush)
|
|
551
|
+
|
|
552
|
+
`smartPush` 是一个智能路由跳转方法,它会自动检测当前环境:
|
|
553
|
+
|
|
554
|
+
- 如果在 iframe 环境中且父级支持 `viewTab`,则自动使用 `viewTab` 进行跳转
|
|
555
|
+
- 否则使用普通的 `router.push` 进行跳转
|
|
556
|
+
|
|
557
|
+
### 基本用法
|
|
558
|
+
|
|
559
|
+
`smartPush` 的参数传递方式与 `router.push` 完全一致:
|
|
560
|
+
|
|
561
|
+
```javascript
|
|
562
|
+
// 字符串路径
|
|
563
|
+
router.smartPush('/home');
|
|
564
|
+
|
|
565
|
+
// 对象格式(与 router.push 一致)
|
|
566
|
+
router.smartPush({ path: '/home', query: { id: 1 } });
|
|
567
|
+
router.smartPush({ name: 'Home', params: { id: 1 } });
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
### iframe 环境中的使用
|
|
571
|
+
|
|
572
|
+
在 iframe 环境中,可以通过 `_viewTab` 选项自定义 viewTab 行为:
|
|
573
|
+
|
|
574
|
+
```javascript
|
|
575
|
+
router.smartPush({
|
|
576
|
+
path: '/preferInfo',
|
|
577
|
+
query: { sourceType: 'insert' },
|
|
578
|
+
params: { custNumber: '701265308', preferId: '1312' },
|
|
579
|
+
_viewTab: {
|
|
580
|
+
title: 'CMC-产品折扣新增', // 标签页标题
|
|
581
|
+
uumsFunction: {
|
|
582
|
+
functionCode: 'CMC_FUNCTION_00002',
|
|
583
|
+
sourceSystem: 'CMC',
|
|
584
|
+
},
|
|
585
|
+
closeCurrentTab: true, // 是否关闭当前标签页
|
|
586
|
+
},
|
|
587
|
+
});
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
### \_viewTab 选项说明
|
|
591
|
+
|
|
592
|
+
- `title` (string, 可选): 标签页标题,默认使用路由 `meta.title` 或 '新页面'
|
|
593
|
+
- `uumsFunction` (object, 可选): UUMS 功能配置
|
|
594
|
+
- `functionCode` (string): 功能代码
|
|
595
|
+
- `sourceSystem` (string): 来源系统
|
|
596
|
+
- `closeCurrentTab` (boolean, 可选): 是否关闭当前标签页,默认为 `false`
|
|
597
|
+
|
|
598
|
+
### 替换原有代码示例
|
|
599
|
+
|
|
600
|
+
**原代码(需要手动判断环境):**
|
|
601
|
+
|
|
602
|
+
```javascript
|
|
603
|
+
if (top.viewTab) {
|
|
604
|
+
let params = { sourceType: 'insert' };
|
|
605
|
+
let url = location.origin + '/#/preferInfo?sourceType=insert';
|
|
606
|
+
let tabUrl = top.Ext.urlAppend(url, 'isUap=true');
|
|
607
|
+
top.closeTab(tabUrl, 'IFRAME');
|
|
608
|
+
top.viewTab('CMC-产品折扣新增', url, 'iframe', params, {
|
|
609
|
+
uumsFunction: {
|
|
610
|
+
functionCode: 'CMC_FUNCTION_00002',
|
|
611
|
+
sourceSystem: 'CMC',
|
|
612
|
+
},
|
|
613
|
+
});
|
|
614
|
+
} else {
|
|
615
|
+
this.$router.push({
|
|
616
|
+
path: '/preferInfo',
|
|
617
|
+
query: { sourceType: 'insert' },
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
**新代码(自动适配):**
|
|
623
|
+
|
|
624
|
+
```javascript
|
|
625
|
+
router.smartPush({
|
|
626
|
+
path: '/preferInfo',
|
|
627
|
+
query: { sourceType: 'insert' },
|
|
628
|
+
params: { sourceType: 'insert' },
|
|
629
|
+
_viewTab: {
|
|
630
|
+
title: 'CMC-产品折扣新增',
|
|
631
|
+
uumsFunction: {
|
|
632
|
+
functionCode: 'CMC_FUNCTION_00002',
|
|
633
|
+
sourceSystem: 'CMC',
|
|
634
|
+
},
|
|
635
|
+
closeCurrentTab: true,
|
|
636
|
+
},
|
|
637
|
+
});
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### smartPush 注意事项
|
|
641
|
+
|
|
642
|
+
1. `smartPush` 内置了 viewTab 功能,无需额外依赖
|
|
643
|
+
2. 如果不在 iframe 环境中,会自动降级为 `router.push`
|
|
644
|
+
3. `_viewTab` 选项仅在 iframe 环境中生效,非 iframe 环境会被忽略
|
|
645
|
+
4. `params` 参数会传递给 `viewTab` 的 `params` 参数,`query` 会转换为 URL 的 query string
|
|
646
|
+
|
|
647
|
+
## 注意事项
|
|
648
|
+
|
|
649
|
+
1. 本包基于 Vue Router 4,需要安装 `vue-router@^4.0.0`
|
|
650
|
+
2. 路由守卫中的 `beforeEach` 如果返回 `false`,会阻止导航
|
|
651
|
+
3. 路由守卫中的 `beforeEach` 如果返回 Promise,会等待 Promise 完成后再导航
|
|
652
|
+
4. 使用 `safePush` 和 `safeReplace` 可以避免导航重复的错误
|
|
653
|
+
5. 路由配置中的 `meta.title` 会自动设置为页面标题
|
|
654
|
+
6. `smartPush` 和 `getQueryByRoute` 已内置 viewTab 相关功能,无需额外依赖
|
|
655
|
+
7. `getQueryByRoute` 在 iframe 环境中会自动合并父级 params 和路由 query 参数,query 参数优先级更高
|
|
656
|
+
8. `hasRoute` 和 `getRouteConfig` 支持检测通过 `router.addRoute()` 动态添加的路由,可以在登录后(如 `onLoggedIn` 回调中)动态添加路由后正常使用
|
|
657
|
+
|
|
658
|
+
## 更多示例
|
|
659
|
+
|
|
660
|
+
更多使用示例请参考项目文档或源码。
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
const env = {};
|
|
3
|
+
try {
|
|
4
|
+
if (process) {
|
|
5
|
+
process.env = Object.assign({}, process.env);
|
|
6
|
+
Object.assign(process.env, env);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
} catch (e) {} // avoid ReferenceError: process is not defined
|
|
10
|
+
globalThis.process = { env:env };
|
|
11
|
+
})();
|