@@ -7,7 +7,7 @@ Designed for vue 3, do the same work like [vue-class-component](https://github.c
77* Community desired vue class component with typescript decorators.
88* Safety. Transform es class to vue option api according to specifications.
99* 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 ` .
1111
1212
1313Welcome to suggest and contribute. Message me on github.
@@ -23,7 +23,7 @@ Welcome to suggest and contribute. Message me on github.
2323### Index
2424
2525* [ Basic] ( #basic )
26- * [ Extends] ( #extends )
26+ * [ Extends and mixins ] ( #extends-and-mixins )
2727* [ Tsx render] ( #tsx-render )
2828* [ In class lifecycle names] ( #in-class-lifecycle-names )
2929
@@ -278,7 +278,7 @@ export default defineComponent({
278278});
279279```
280280
281- ### Extends
281+ ### Extends and mixins
282282
283283``` typescript
284284import { Component , ComponentBase , Base } from ' vue-facing-decorator'
@@ -317,12 +317,20 @@ class Comp2 extends Comp2Sup {
317317 method2Comp() {
318318 return ' method2Comp value'
319319 }
320+
321+ sameMethodName(){
322+ console .log (' in Comp2' )
323+ }
320324}
321325
322326class Comp3Sup extends Comp2 {
323327 method3Sup() {
324328 return ' method3Sup value'
325329 }
330+
331+ sameMethodName(){
332+ console .log (' in Comp3Sup' )
333+ }
326334}
327335
328336/*
@@ -335,40 +343,169 @@ Vue component extends vue component by vue component exteding strategy i.e. `(Co
335343
336344`Comp3` is a "Final Component" decorated by '@Component'.
337345*/
346+
347+ /*
348+ The sameMethodName method is the one in Comp3.
349+ The priority is Comp3 > Comp3Sup > Comp2
350+ */
338351@Component
339- export default class Comp3 extends Comp3Sup {
352+ export class Comp3 extends Comp3Sup {
340353 method3Comp() {
341354 return ' method3Comp value'
342355 }
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+ }
343400}
344401```
345402
346403is euqal to
347404
348405``` typescript
406+ // For Comp3
349407import { 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'
357417 }
418+ }
358419 },
359420 methods: {
421+ method2Sup() {
422+ return ' method2Sup value'
423+ },
360424 method2Comp() {
361425 return ' method2Comp value'
426+ },
427+ sameMethodName(){
428+ console .log (' in Comp2' )
362429 }
363430 }
364431 },
365432 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+ }
369442 }
370443})
371444
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+ })
372509```
373510
374511### Tsx render
0 commit comments