@lytjs/compat 6.4.0 → 6.6.0
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/README.md +743 -0
- package/package.json +52 -52
package/README.md
ADDED
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
# @lytjs/compat
|
|
2
|
+
|
|
3
|
+
> LytJS Vue 2/3 兼容性层,为从 Vue 项目迁移到 LytJS 提供平滑过渡支持。
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@lytjs/compat)
|
|
6
|
+
[](https://gitee.com/lytjs/lytjs/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## 简介
|
|
9
|
+
|
|
10
|
+
`@lytjs/compat` 是 LytJS 框架的官方兼容性层包,旨在帮助开发团队从 Vue 2 或 Vue 3 项目平滑迁移到 LytJS。它提供了一系列兼容性适配器、生命周期钩子映射、状态管理兼容层和路由兼容工具,最大程度地减少迁移成本。
|
|
11
|
+
|
|
12
|
+
### 核心特性
|
|
13
|
+
|
|
14
|
+
- **生命周期兼容**:自动映射 Vue 2/3 生命周期钩子到 LytJS
|
|
15
|
+
- **Vuex 兼容层**:`mapState`、`mapGetters`、`mapActions` 等工具函数
|
|
16
|
+
- **Vue Router 兼容**:路由守卫命名和参数映射
|
|
17
|
+
- **响应式 API 兼容**:Vue 响应式 API 到 LytJS Signal 的桥接
|
|
18
|
+
- **零运行时开销**:生产环境自动剥离兼容代码
|
|
19
|
+
- **渐进式迁移**:支持部分迁移,逐步替换
|
|
20
|
+
|
|
21
|
+
## 安装
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @lytjs/compat
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
或使用 pnpm:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @lytjs/compat
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 依赖关系
|
|
34
|
+
|
|
35
|
+
`@lytjs/compat` 依赖以下 LytJS 核心包:
|
|
36
|
+
|
|
37
|
+
- `@lytjs/core` - 核心运行时
|
|
38
|
+
- `@lytjs/component` - 组件系统
|
|
39
|
+
- `@lytjs/reactivity` - 响应式系统
|
|
40
|
+
|
|
41
|
+
## 快速开始
|
|
42
|
+
|
|
43
|
+
### 启用兼容性模式
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { enableCompat } from '@lytjs/compat';
|
|
47
|
+
|
|
48
|
+
enableCompat({
|
|
49
|
+
vue2Mode: true,
|
|
50
|
+
vue3Mode: false,
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 在组件中使用 Vue 生命周期
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { defineComponent } from '@lytjs/compat';
|
|
58
|
+
|
|
59
|
+
export const MyComponent = defineComponent({
|
|
60
|
+
// Vue 2/3 生命周期钩子自动映射到 LytJS
|
|
61
|
+
beforeCreate() {
|
|
62
|
+
console.log('组件创建前');
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
created() {
|
|
66
|
+
console.log('组件已创建');
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
beforeMount() {
|
|
70
|
+
console.log('组件挂载前');
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
mounted() {
|
|
74
|
+
console.log('组件已挂载');
|
|
75
|
+
this.$refs.input.focus();
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
beforeUpdate() {
|
|
79
|
+
console.log('组件更新前');
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
updated() {
|
|
83
|
+
console.log('组件已更新');
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
beforeUnmount() {
|
|
87
|
+
console.log('组件卸载前');
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
unmounted() {
|
|
91
|
+
console.log('组件已卸载');
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
render() {
|
|
95
|
+
return <div>Hello LytJS</div>;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 生命周期钩子映射
|
|
101
|
+
|
|
102
|
+
### Vue 2 → LytJS 映射
|
|
103
|
+
|
|
104
|
+
| Vue 2 钩子 | LytJS 对应 |
|
|
105
|
+
| --------------- | ----------------- |
|
|
106
|
+
| `beforeCreate` | `onBeforeInit` |
|
|
107
|
+
| `created` | `onInit` |
|
|
108
|
+
| `beforeMount` | `onBeforeMount` |
|
|
109
|
+
| `mounted` | `onMounted` |
|
|
110
|
+
| `beforeUpdate` | `onBeforeUpdate` |
|
|
111
|
+
| `updated` | `onUpdated` |
|
|
112
|
+
| `beforeDestroy` | `onBeforeUnmount` |
|
|
113
|
+
| `destroyed` | `onUnmounted` |
|
|
114
|
+
| `errorCaptured` | `onErrorCaptured` |
|
|
115
|
+
|
|
116
|
+
### Vue 3 → LytJS 映射
|
|
117
|
+
|
|
118
|
+
| Vue 3 钩子 | LytJS 对应 |
|
|
119
|
+
| ------------------- | ------------------- |
|
|
120
|
+
| `setup` | `onInit` |
|
|
121
|
+
| `onMounted` | `onMounted` |
|
|
122
|
+
| `onUpdated` | `onUpdated` |
|
|
123
|
+
| `onUnmounted` | `onUnmounted` |
|
|
124
|
+
| `onErrorCaptured` | `onErrorCaptured` |
|
|
125
|
+
| `onRenderTracked` | `onRenderTracked` |
|
|
126
|
+
| `onRenderTriggered` | `onRenderTriggered` |
|
|
127
|
+
|
|
128
|
+
### 使用示例
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { defineComponent } from '@lytjs/compat';
|
|
132
|
+
|
|
133
|
+
export const DataFetcher = defineComponent({
|
|
134
|
+
data() {
|
|
135
|
+
return {
|
|
136
|
+
users: [],
|
|
137
|
+
loading: false
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
async created() {
|
|
142
|
+
this.loading = true;
|
|
143
|
+
this.users = await fetchUsers();
|
|
144
|
+
this.loading = false;
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
beforeDestroy() {
|
|
148
|
+
console.log('清理资源');
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
render() {
|
|
152
|
+
if (this.loading) {
|
|
153
|
+
return <div>加载中...</div>;
|
|
154
|
+
}
|
|
155
|
+
return (
|
|
156
|
+
<ul>
|
|
157
|
+
{this.users.map(user => (
|
|
158
|
+
<li key={user.id}>{user.name}</li>
|
|
159
|
+
))}
|
|
160
|
+
</ul>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Vuex 兼容层
|
|
167
|
+
|
|
168
|
+
### mapState
|
|
169
|
+
|
|
170
|
+
将 Vuex store 状态映射到组件。
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { mapState } from '@lytjs/compat';
|
|
174
|
+
import { useStore } from '@lytjs/store';
|
|
175
|
+
|
|
176
|
+
export const UserProfile = defineComponent({
|
|
177
|
+
computed: {
|
|
178
|
+
...mapState('user', {
|
|
179
|
+
userName: 'name',
|
|
180
|
+
userEmail: 'email',
|
|
181
|
+
isAdmin: (state) => state.permissions.includes('admin')
|
|
182
|
+
})
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
render() {
|
|
186
|
+
return (
|
|
187
|
+
<div>
|
|
188
|
+
<p>姓名: {this.userName}</p>
|
|
189
|
+
<p>邮箱: {this.userEmail}</p>
|
|
190
|
+
{this.isAdmin && <span>管理员</span>}
|
|
191
|
+
</div>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### mapGetters
|
|
198
|
+
|
|
199
|
+
映射 Vuex getters。
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { mapGetters } from '@lytjs/compat';
|
|
203
|
+
|
|
204
|
+
export const ProductList = defineComponent({
|
|
205
|
+
computed: {
|
|
206
|
+
...mapGetters('product', {
|
|
207
|
+
sortedProducts: 'sortedList',
|
|
208
|
+
totalPrice: 'cartTotal'
|
|
209
|
+
})
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
render() {
|
|
213
|
+
return (
|
|
214
|
+
<div>
|
|
215
|
+
<p>总价: ¥{this.totalPrice}</p>
|
|
216
|
+
{this.sortedProducts.map(p => (
|
|
217
|
+
<ProductItem key={p.id} product={p} />
|
|
218
|
+
))}
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### mapActions
|
|
226
|
+
|
|
227
|
+
映射 Vuex actions。
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import { mapActions } from '@lytjs/compat';
|
|
231
|
+
|
|
232
|
+
export const LoginForm = defineComponent({
|
|
233
|
+
data() {
|
|
234
|
+
return {
|
|
235
|
+
email: '',
|
|
236
|
+
password: ''
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
methods: {
|
|
241
|
+
...mapActions('user', {
|
|
242
|
+
login: 'login',
|
|
243
|
+
logout: 'logout'
|
|
244
|
+
}),
|
|
245
|
+
|
|
246
|
+
async handleSubmit() {
|
|
247
|
+
await this.login({
|
|
248
|
+
email: this.email,
|
|
249
|
+
password: this.password
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
render() {
|
|
255
|
+
return (
|
|
256
|
+
<form onSubmit={this.handleSubmit}>
|
|
257
|
+
<input v-model={this.email} type="email" />
|
|
258
|
+
<input v-model={this.password} type="password" />
|
|
259
|
+
<button type="submit">登录</button>
|
|
260
|
+
</form>
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### mapMutations
|
|
267
|
+
|
|
268
|
+
映射 Vuex mutations。
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import { mapMutations } from '@lytjs/compat';
|
|
272
|
+
|
|
273
|
+
export const Counter = defineComponent({
|
|
274
|
+
data() {
|
|
275
|
+
return { count: 0 };
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
methods: {
|
|
279
|
+
...mapMutations('counter', {
|
|
280
|
+
increment: 'INCREMENT',
|
|
281
|
+
decrement: 'DECREMENT'
|
|
282
|
+
})
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
render() {
|
|
286
|
+
return (
|
|
287
|
+
<div>
|
|
288
|
+
<span>{this.count}</span>
|
|
289
|
+
<button onClick={() => this.increment()}>+</button>
|
|
290
|
+
<button onClick={() => this.decrement()}>-</button>
|
|
291
|
+
</div>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### createNamespacedHelpers
|
|
298
|
+
|
|
299
|
+
创建命名空间辅助函数。
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { createNamespacedHelpers } from '@lytjs/compat';
|
|
303
|
+
|
|
304
|
+
const { mapState, mapActions } = createNamespacedHelpers('cart');
|
|
305
|
+
|
|
306
|
+
export const CartItems = defineComponent({
|
|
307
|
+
computed: {
|
|
308
|
+
...mapState({
|
|
309
|
+
items: 'items',
|
|
310
|
+
total: 'totalPrice'
|
|
311
|
+
})
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
methods: {
|
|
315
|
+
...mapActions(['addItem', 'removeItem'])
|
|
316
|
+
},
|
|
317
|
+
|
|
318
|
+
render() {
|
|
319
|
+
return (
|
|
320
|
+
<div>
|
|
321
|
+
{this.items.map(item => (
|
|
322
|
+
<CartItem
|
|
323
|
+
key={item.id}
|
|
324
|
+
item={item}
|
|
325
|
+
onRemove={() => this.removeItem(item.id)}
|
|
326
|
+
/>
|
|
327
|
+
))}
|
|
328
|
+
<p>总计: ¥{this.total}</p>
|
|
329
|
+
</div>
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## Vue Router 兼容
|
|
336
|
+
|
|
337
|
+
### 路由守卫命名映射
|
|
338
|
+
|
|
339
|
+
| Vue Router | LytJS Router |
|
|
340
|
+
| ------------------- | --------------------- |
|
|
341
|
+
| `beforeEach` | `beforeEach` |
|
|
342
|
+
| `beforeResolve` | `beforeResolve` |
|
|
343
|
+
| `afterEach` | `afterEach` |
|
|
344
|
+
| `beforeRouteEnter` | `onBeforeRouteEnter` |
|
|
345
|
+
| `beforeRouteUpdate` | `onBeforeRouteUpdate` |
|
|
346
|
+
| `beforeRouteLeave` | `onBeforeRouteLeave` |
|
|
347
|
+
|
|
348
|
+
### 使用示例
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
import { defineComponent, onBeforeRouteEnter, onBeforeRouteLeave } from '@lytjs/compat';
|
|
352
|
+
|
|
353
|
+
export const UserEdit = defineComponent({
|
|
354
|
+
data() {
|
|
355
|
+
return {
|
|
356
|
+
hasChanges: false
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
onBeforeRouteEnter(to, from, next) {
|
|
361
|
+
if (this.hasChanges) {
|
|
362
|
+
const answer = window.confirm('有未保存的更改,确定离开吗?');
|
|
363
|
+
if (answer) {
|
|
364
|
+
next();
|
|
365
|
+
} else {
|
|
366
|
+
next(false);
|
|
367
|
+
}
|
|
368
|
+
} else {
|
|
369
|
+
next();
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
|
|
373
|
+
onBeforeRouteLeave(to, from, next) {
|
|
374
|
+
if (this.hasChanges) {
|
|
375
|
+
const answer = window.confirm('有未保存的更改,确定离开吗?');
|
|
376
|
+
next(answer);
|
|
377
|
+
} else {
|
|
378
|
+
next();
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
render() {
|
|
383
|
+
return <div>用户编辑</div>;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
## 响应式 API 兼容
|
|
389
|
+
|
|
390
|
+
### reactive 和 ref
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
import { ref, reactive } from '@lytjs/compat';
|
|
394
|
+
|
|
395
|
+
export const ReactiveDemo = defineComponent({
|
|
396
|
+
setup() {
|
|
397
|
+
const count = ref(0);
|
|
398
|
+
const state = reactive({
|
|
399
|
+
name: 'LytJS',
|
|
400
|
+
version: '6.0'
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
function increment() {
|
|
404
|
+
count.value++;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return { count, state, increment };
|
|
408
|
+
},
|
|
409
|
+
|
|
410
|
+
render() {
|
|
411
|
+
return (
|
|
412
|
+
<div>
|
|
413
|
+
<p>计数: {this.count}</p>
|
|
414
|
+
<p>名称: {this.state.name}</p>
|
|
415
|
+
<p>版本: {this.state.version}</p>
|
|
416
|
+
<button onClick={() => this.increment()}>增加</button>
|
|
417
|
+
</div>
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### computed
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
import { ref, computed } from '@lytjs/compat';
|
|
427
|
+
|
|
428
|
+
export const ComputedDemo = defineComponent({
|
|
429
|
+
setup() {
|
|
430
|
+
const firstName = ref('张');
|
|
431
|
+
const lastName = ref('三');
|
|
432
|
+
|
|
433
|
+
const fullName = computed(() => `${firstName.value} ${lastName.value}`);
|
|
434
|
+
const nameLength = computed(() => fullName.value.length);
|
|
435
|
+
|
|
436
|
+
return { firstName, lastName, fullName, nameLength };
|
|
437
|
+
},
|
|
438
|
+
|
|
439
|
+
render() {
|
|
440
|
+
return (
|
|
441
|
+
<div>
|
|
442
|
+
<p>姓名: {this.fullName}</p>
|
|
443
|
+
<p>长度: {this.nameLength}</p>
|
|
444
|
+
</div>
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### watch 和 watchEffect
|
|
451
|
+
|
|
452
|
+
```typescript
|
|
453
|
+
import { ref, watch, watchEffect } from '@lytjs/compat';
|
|
454
|
+
|
|
455
|
+
export const WatchDemo = defineComponent({
|
|
456
|
+
setup() {
|
|
457
|
+
const count = ref(0);
|
|
458
|
+
const userId = ref('123');
|
|
459
|
+
|
|
460
|
+
watch(count, (newVal, oldVal) => {
|
|
461
|
+
console.log(`count 从 ${oldVal} 变为 ${newVal}`);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
watchEffect(() => {
|
|
465
|
+
console.log(`当前 count: ${count.value}`);
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
return { count };
|
|
469
|
+
},
|
|
470
|
+
|
|
471
|
+
render() {
|
|
472
|
+
return (
|
|
473
|
+
<div>
|
|
474
|
+
<p>计数: {this.count}</p>
|
|
475
|
+
<button onClick={() => this.count.value++}>增加</button>
|
|
476
|
+
</div>
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## $emit 兼容
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
import { defineComponent } from '@lytjs/compat';
|
|
486
|
+
|
|
487
|
+
export const CustomButton = defineComponent({
|
|
488
|
+
props: {
|
|
489
|
+
type: { type: String, default: 'button' }
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
methods: {
|
|
493
|
+
handleClick(event) {
|
|
494
|
+
this.$emit('click', event);
|
|
495
|
+
this.$emit('custom-event', { timestamp: Date.now() });
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
|
|
499
|
+
render() {
|
|
500
|
+
return (
|
|
501
|
+
<button type={this.type} onClick={this.handleClick}>
|
|
502
|
+
{this.$slots.default?.()}
|
|
503
|
+
</button>
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
export const Parent = defineComponent({
|
|
509
|
+
render() {
|
|
510
|
+
return (
|
|
511
|
+
<CustomButton onClick={(e) => console.log('点击', e)}>
|
|
512
|
+
点击我
|
|
513
|
+
</CustomButton>
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
## $refs 兼容
|
|
520
|
+
|
|
521
|
+
```typescript
|
|
522
|
+
import { defineComponent } from '@lytjs/compat';
|
|
523
|
+
|
|
524
|
+
export const FocusInput = defineComponent({
|
|
525
|
+
mounted() {
|
|
526
|
+
this.$refs.input.focus();
|
|
527
|
+
},
|
|
528
|
+
|
|
529
|
+
render() {
|
|
530
|
+
return <input ref="input" type="text" />;
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
## $slots 兼容
|
|
536
|
+
|
|
537
|
+
```typescript
|
|
538
|
+
import { defineComponent } from '@lytjs/compat';
|
|
539
|
+
|
|
540
|
+
export const Card = defineComponent({
|
|
541
|
+
props: {
|
|
542
|
+
title: { type: String }
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
render() {
|
|
546
|
+
return (
|
|
547
|
+
<div class="card">
|
|
548
|
+
<div class="card-header">
|
|
549
|
+
<h2>{this.title}</h2>
|
|
550
|
+
</div>
|
|
551
|
+
<div class="card-body">
|
|
552
|
+
{this.$slots.default?.()}
|
|
553
|
+
</div>
|
|
554
|
+
<div class="card-footer">
|
|
555
|
+
{this.$slots.footer?.()}
|
|
556
|
+
</div>
|
|
557
|
+
</div>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
export const Parent = defineComponent({
|
|
563
|
+
render() {
|
|
564
|
+
return (
|
|
565
|
+
<Card title="我的卡片">
|
|
566
|
+
<p>卡片内容</p>
|
|
567
|
+
<template #footer>
|
|
568
|
+
<button>确定</button>
|
|
569
|
+
</template>
|
|
570
|
+
</Card>
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
## 主要 API
|
|
577
|
+
|
|
578
|
+
### `enableCompat(options)`
|
|
579
|
+
|
|
580
|
+
启用兼容性模式。
|
|
581
|
+
|
|
582
|
+
```typescript
|
|
583
|
+
import { enableCompat } from '@lytjs/compat';
|
|
584
|
+
|
|
585
|
+
enableCompat({
|
|
586
|
+
vue2Mode: true,
|
|
587
|
+
vue3Mode: false,
|
|
588
|
+
silent: false,
|
|
589
|
+
});
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### `defineComponent(options)`
|
|
593
|
+
|
|
594
|
+
定义兼容组件。
|
|
595
|
+
|
|
596
|
+
```typescript
|
|
597
|
+
import { defineComponent } from '@lytjs/compat';
|
|
598
|
+
|
|
599
|
+
const MyComponent = defineComponent({
|
|
600
|
+
data() {
|
|
601
|
+
/* ... */
|
|
602
|
+
},
|
|
603
|
+
methods: {
|
|
604
|
+
/* ... */
|
|
605
|
+
},
|
|
606
|
+
computed: {
|
|
607
|
+
/* ... */
|
|
608
|
+
},
|
|
609
|
+
render() {
|
|
610
|
+
/* ... */
|
|
611
|
+
},
|
|
612
|
+
});
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### `mapState(namespace, map)`
|
|
616
|
+
|
|
617
|
+
映射状态到计算属性。
|
|
618
|
+
|
|
619
|
+
```typescript
|
|
620
|
+
import { mapState } from '@lytjs/compat';
|
|
621
|
+
|
|
622
|
+
export default {
|
|
623
|
+
computed: {
|
|
624
|
+
...mapState('user', ['name', 'email']),
|
|
625
|
+
...mapState('cart', {
|
|
626
|
+
cartCount: 'items.length',
|
|
627
|
+
isEmpty: (state) => state.items.length === 0,
|
|
628
|
+
}),
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
### `mapGetters(namespace, map)`
|
|
634
|
+
|
|
635
|
+
映射 getters。
|
|
636
|
+
|
|
637
|
+
```typescript
|
|
638
|
+
import { mapGetters } from '@lytjs/compat';
|
|
639
|
+
|
|
640
|
+
export default {
|
|
641
|
+
computed: {
|
|
642
|
+
...mapGetters('product', ['sortedList', 'featuredItems']),
|
|
643
|
+
},
|
|
644
|
+
};
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
### `mapActions(namespace, map)`
|
|
648
|
+
|
|
649
|
+
映射 actions。
|
|
650
|
+
|
|
651
|
+
```typescript
|
|
652
|
+
import { mapActions } from '@lytjs/compat';
|
|
653
|
+
|
|
654
|
+
export default {
|
|
655
|
+
methods: {
|
|
656
|
+
...mapActions('user', ['login', 'logout']),
|
|
657
|
+
...mapActions('cart', {
|
|
658
|
+
addToCart: 'addItem',
|
|
659
|
+
removeFromCart: 'removeItem',
|
|
660
|
+
}),
|
|
661
|
+
},
|
|
662
|
+
};
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
### `mapMutations(namespace, map)`
|
|
666
|
+
|
|
667
|
+
映射 mutations。
|
|
668
|
+
|
|
669
|
+
```typescript
|
|
670
|
+
import { mapMutations } from '@lytjs/compat';
|
|
671
|
+
|
|
672
|
+
export default {
|
|
673
|
+
methods: {
|
|
674
|
+
...mapMutations('counter', ['INCREMENT', 'DECREMENT']),
|
|
675
|
+
},
|
|
676
|
+
};
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
### `createNamespacedHelpers(namespace)`
|
|
680
|
+
|
|
681
|
+
创建命名空间辅助函数。
|
|
682
|
+
|
|
683
|
+
```typescript
|
|
684
|
+
import { createNamespacedHelpers } from '@lytjs/compat';
|
|
685
|
+
|
|
686
|
+
const { mapState, mapGetters, mapActions } = createNamespacedHelpers('module');
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
## 配置选项
|
|
690
|
+
|
|
691
|
+
```typescript
|
|
692
|
+
interface CompatOptions {
|
|
693
|
+
vue2Mode?: boolean;
|
|
694
|
+
vue3Mode?: boolean;
|
|
695
|
+
silent?: boolean;
|
|
696
|
+
onWarn?: (message: string) => void;
|
|
697
|
+
}
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
## 迁移策略
|
|
701
|
+
|
|
702
|
+
### 渐进式迁移
|
|
703
|
+
|
|
704
|
+
```typescript
|
|
705
|
+
// 1. 安装兼容包
|
|
706
|
+
npm install @lytjs/compat
|
|
707
|
+
|
|
708
|
+
// 2. 在入口启用兼容模式
|
|
709
|
+
import { enableCompat } from '@lytjs/compat';
|
|
710
|
+
enableCompat();
|
|
711
|
+
|
|
712
|
+
// 3. 逐个组件迁移
|
|
713
|
+
// 从简单组件开始,逐步迁移复杂组件
|
|
714
|
+
|
|
715
|
+
// 4. 移除兼容代码
|
|
716
|
+
// 迁移完成后,可选择性移除兼容包
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
### 组件迁移检查表
|
|
720
|
+
|
|
721
|
+
- [ ] 替换 `data()` 为响应式变量
|
|
722
|
+
- [ ] 替换 `methods` 为普通函数
|
|
723
|
+
- [ ] 替换 `computed` 为 `computed()`
|
|
724
|
+
- [ ] 替换 `watch` 为 `watch()`
|
|
725
|
+
- [ ] 替换 `beforeDestroy` 为 `onBeforeUnmount`
|
|
726
|
+
- [ ] 替换 `Vuex` 为 `@lytjs/store`
|
|
727
|
+
- [ ] 替换 `$emit` 为 `emit()`
|
|
728
|
+
- [ ] 清理 `this` 引用
|
|
729
|
+
|
|
730
|
+
## 浏览器兼容性
|
|
731
|
+
|
|
732
|
+
`@lytjs/compat` 支持所有现代浏览器。
|
|
733
|
+
|
|
734
|
+
## 许可证
|
|
735
|
+
|
|
736
|
+
MIT License - [查看许可证](https://gitee.com/lytjs/lytjs/blob/main/LICENSE)
|
|
737
|
+
|
|
738
|
+
## 贡献指南
|
|
739
|
+
|
|
740
|
+
欢迎提交 Issue 和 Pull Request!
|
|
741
|
+
|
|
742
|
+
- [Gitee 仓库](https://gitee.com/lytjs/lytjs)
|
|
743
|
+
- [问题反馈](https://gitee.com/lytjs/lytjs/issues)
|
package/package.json
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@lytjs/compat",
|
|
3
|
-
"version": "6.
|
|
4
|
-
"description": "LytJS Vue 2/3 compatibility layer for migration support",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
},
|
|
15
|
-
"./package.json": "./package.json"
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist"
|
|
19
|
-
],
|
|
20
|
-
"sideEffects": false,
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsup",
|
|
23
|
-
"dev": "tsup --watch",
|
|
24
|
-
"test": "vitest run",
|
|
25
|
-
"test:watch": "vitest",
|
|
26
|
-
"type-check": "tsc --noEmit",
|
|
27
|
-
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
28
|
-
"clean": "rm -rf dist"
|
|
29
|
-
},
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"@lytjs/reactivity": "
|
|
32
|
-
"@lytjs/component": "
|
|
33
|
-
"@lytjs/core": "
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"tsup": "^8.0.0",
|
|
37
|
-
"typescript": "^5.4.0",
|
|
38
|
-
"vitest": "^3.0.0"
|
|
39
|
-
},
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
44
|
-
"directory": "packages/ecosystem/packages/compat"
|
|
45
|
-
},
|
|
46
|
-
"keywords": [
|
|
47
|
-
"lytjs",
|
|
48
|
-
"compat",
|
|
49
|
-
"migration",
|
|
50
|
-
"vue"
|
|
51
|
-
]
|
|
52
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@lytjs/compat",
|
|
3
|
+
"version": "6.6.0",
|
|
4
|
+
"description": "LytJS Vue 2/3 compatibility layer for migration support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"type-check": "tsc --noEmit",
|
|
27
|
+
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
28
|
+
"clean": "rm -rf dist"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@lytjs/reactivity": "workspace:*",
|
|
32
|
+
"@lytjs/component": "workspace:*",
|
|
33
|
+
"@lytjs/core": "workspace:*"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"tsup": "^8.0.0",
|
|
37
|
+
"typescript": "^5.4.0",
|
|
38
|
+
"vitest": "^3.0.0"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
44
|
+
"directory": "packages/ecosystem/packages/compat"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"lytjs",
|
|
48
|
+
"compat",
|
|
49
|
+
"migration",
|
|
50
|
+
"vue"
|
|
51
|
+
]
|
|
52
|
+
}
|