@@ -7,7 +7,7 @@ Designed for vue 3, do the same work like [vue-class-component](https://github.c
7
7
* Community desired vue class component with typescript decorators.
8
8
* Safety. Transform es class to vue option api according to specifications.
9
9
* Performance. Once transform on project loading, ready for everywhere.
10
- * Support both es class inherit and vue component extending .
10
+ * Support es class inherit, vue component ` extends ` and vue ` mixins ` .
11
11
12
12
13
13
Welcome to suggest and contribute. Message me on github.
@@ -23,7 +23,7 @@ Welcome to suggest and contribute. Message me on github.
23
23
### Index
24
24
25
25
* [ Basic] ( #basic )
26
- * [ Extends] ( #extends )
26
+ * [ Extends and mixins ] ( #extends-and-mixins )
27
27
* [ Tsx render] ( #tsx-render )
28
28
* [ In class lifecycle names] ( #in-class-lifecycle-names )
29
29
@@ -278,7 +278,7 @@ export default defineComponent({
278
278
});
279
279
```
280
280
281
- ### Extends
281
+ ### Extends and mixins
282
282
283
283
``` typescript
284
284
import { Component , ComponentBase , Base } from ' vue-facing-decorator'
@@ -317,12 +317,20 @@ class Comp2 extends Comp2Sup {
317
317
method2Comp() {
318
318
return ' method2Comp value'
319
319
}
320
+
321
+ sameMethodName(){
322
+ console .log (' in Comp2' )
323
+ }
320
324
}
321
325
322
326
class Comp3Sup extends Comp2 {
323
327
method3Sup() {
324
328
return ' method3Sup value'
325
329
}
330
+
331
+ sameMethodName(){
332
+ console .log (' in Comp3Sup' )
333
+ }
326
334
}
327
335
328
336
/*
@@ -335,40 +343,169 @@ Vue component extends vue component by vue component exteding strategy i.e. `(Co
335
343
336
344
`Comp3` is a "Final Component" decorated by '@Component'.
337
345
*/
346
+
347
+ /*
348
+ The sameMethodName method is the one in Comp3.
349
+ The priority is Comp3 > Comp3Sup > Comp2
350
+ */
338
351
@Component
339
- export default class Comp3 extends Comp3Sup {
352
+ export class Comp3 extends Comp3Sup {
340
353
method3Comp() {
341
354
return ' method3Comp value'
342
355
}
356
+
357
+ sameMethodName(){
358
+ console .log (' in Comp3' )
359
+ }
360
+ }
361
+
362
+ /*
363
+ If `mixins` is provided to Component decorator, it will be setted to vue option api `mixins`. Both `extends` and `mixins` will be effective.
364
+ Mixins component will lost them type information. So wo should cast `this` context to `any`.
365
+ In this case, the sameMethodName method is the one in Comp3.
366
+ The priority is Comp3 > Comp3Sup > mixin2 > mixin1 > Comp2
367
+ Comp3 and Comp3Sup considered one component. It `mixins: [mixin1,mixin2]` and `extends: Comp2`
368
+ */
369
+ @Component ({
370
+ mixins:[{
371
+ methods:{
372
+ mixin1Method(){
373
+ },
374
+ sameMethodName(){
375
+ console .log (' in mixin1' )
376
+ }
377
+ }
378
+ },{
379
+ methods:{
380
+ mixin2Method(){
381
+ },
382
+ sameMethodName(){
383
+ console .log (' in mixin2' )
384
+ }
385
+ }
386
+ }]
387
+ })
388
+ export class Comp3Mixins extends Comp3Sup {
389
+ sameMethodName(){
390
+ console .log (' in Comp3Mixins' )
391
+ }
392
+
393
+ get mixinContext(): any { // Mixins will lost type information, for now return any this
394
+ return this
395
+ }
396
+ mounted(){
397
+ this .mixinContext .mixin1Method ()
398
+ this .mixinContext .mixin2Method ()
399
+ }
343
400
}
344
401
```
345
402
346
403
is euqal to
347
404
348
405
``` typescript
406
+ // For Comp3
349
407
import { defineComponent } from ' vue' ;
350
- export default defineComponent ({
351
- extends: {
352
- extends: {
353
- methods: {
354
- method1Comp() {
355
- return ' method1Comp value'
356
- }
408
+ defineComponent ({
409
+ extends: {// This is Comp2 includes Comp2Sup
410
+ extends: {// This is Comp2 includes Comp3Sup
411
+ methods: {
412
+ method1Sup() {
413
+ return ' method1Sup value'
414
+ },
415
+ method1Comp() {
416
+ return ' method1Comp value'
357
417
}
418
+ }
358
419
},
359
420
methods: {
421
+ method2Sup() {
422
+ return ' method2Sup value'
423
+ },
360
424
method2Comp() {
361
425
return ' method2Comp value'
426
+ },
427
+ sameMethodName(){
428
+ console .log (' in Comp2' )
362
429
}
363
430
}
364
431
},
365
432
methods: {
366
- method3Comp() {
367
- return ' method3Comp value'
368
- }
433
+ method3Sup() {
434
+ return ' method3Sup value'
435
+ },
436
+ method3Comp() {
437
+ return ' method3Comp value'
438
+ },
439
+ sameMethodName(){// This method in Comp3 overwrites the one in Comp3Sup
440
+ console .log (' in Comp3' )
441
+ }
369
442
}
370
443
})
371
444
445
+ // For Comp3Mixins
446
+
447
+ defineComponent ({
448
+ mixins:[{
449
+ methods:{
450
+ mixin1Method(){
451
+ },
452
+ sameMethodName(){
453
+ console .log (' in mixin1' )
454
+ }
455
+ }
456
+ },{
457
+ methods:{
458
+ mixin2Method(){
459
+ },
460
+ sameMethodName(){
461
+ console .log (' in mixin2' )
462
+ }
463
+ }
464
+ }],
465
+ extends: {
466
+ extends: {
467
+ methods: {
468
+ method1Sup() {
469
+ return ' method1Sup value'
470
+ },
471
+ method1Comp() {
472
+ return ' method1Comp value'
473
+ }
474
+ }
475
+ },
476
+ methods: {
477
+ method2Sup() {
478
+ return ' method2Sup value'
479
+ },
480
+ method2Comp() {
481
+ return ' method2Comp value'
482
+ },
483
+ sameMethodName(){
484
+ console .log (' in Comp2' )
485
+ }
486
+ }
487
+ },
488
+ methods: {
489
+ method3Sup() {
490
+ return ' method3Sup value'
491
+ },
492
+ method3Comp() {
493
+ return ' method3Comp value'
494
+ },
495
+ sameMethodName(){
496
+ console .log (' in Comp3' )
497
+ }
498
+ },
499
+ computed:{
500
+ mixinContext(){
501
+ return this
502
+ }
503
+ },
504
+ mounted(){
505
+ this .mixinContext .mixin1Method ()
506
+ this .mixinContext .mixin2Method ()
507
+ }
508
+ })
372
509
```
373
510
374
511
### Tsx render
0 commit comments