11import '@abraham/reflection'
2+ import type { ClassType } from '@/index'
23import { Autobind , Component , ComponentProps , Computed , Hook , Link , Ref , VueComponent , VueService } from '@/index'
34import { forwardRef , Inject , Injectable , SkipSelf } from 'injection-js'
4- import { createApp , watch } from 'vue'
5+ import { createApp , VNodeChild , watch } from 'vue'
56
67// 服务,即可复用的逻辑 类似 useXXX
78@Injectable ( )
@@ -22,6 +23,10 @@ class CountService extends VueService {
2223// 组件属性声明
2324interface HomeChild_Props {
2425 size : 'small' | 'large'
26+ // 组件个性化定义属性
27+ slots : {
28+ item ( name : string ) : VNodeChild
29+ }
2530}
2631
2732// 带属性组件
@@ -34,7 +39,7 @@ class HomeChild extends VueComponent<HomeChild_Props> {
3439 constructor (
3540 private countService : CountService ,
3641 @SkipSelf ( ) private parentCountService : CountService ,
37- @Inject ( forwardRef ( ( ) => Home ) ) private parent : InstanceType < typeof Home > ,
42+ @Inject ( forwardRef ( ( ) => Home ) ) private parent : ClassType < Home > ,
3843 ) {
3944 super ( )
4045 }
@@ -50,12 +55,16 @@ class HomeChild extends VueComponent<HomeChild_Props> {
5055 < button onClick = { this . countService . add } > +</ button >
5156 { this . countService . count }
5257 < button onClick = { this . countService . remove } > -</ button >
58+ < div >
59+ < h3 > 这里是组件定制化内容</ h3 >
60+ { this . context . slots . item ?.( '我是定制化内容' ) }
61+ </ div >
5362 </ div >
5463 )
5564 }
5665}
57-
5866// 组件
67+ @Autobind ( ) // 绑定this 也可以放到这里
5968@Component ( { providers : [ CountService ] } ) // 声明自己的服务
6069class Home extends VueComponent {
6170 // 构造函数注入服务,无需new
@@ -83,14 +92,12 @@ class Home extends VueComponent {
8392 }
8493
8594 // 子组件引用 链接🔗
86- @Link ( ) child ?: HomeChild
95+ @Link ( ) child ?: ClassType < HomeChild >
8796
88- @Autobind ( )
8997 add ( ) {
9098 this . count ++
9199 }
92100
93- @Autobind ( )
94101 remove ( ) {
95102 this . count --
96103 }
@@ -108,7 +115,15 @@ class Home extends VueComponent {
108115 { this . count }
109116 < button onClick = { this . remove } > -</ button >
110117
111- < HomeChild ref = "child" size = { 'small' } > </ HomeChild >
118+ < HomeChild
119+ ref = "child"
120+ size = { 'small' }
121+ v-slots = { {
122+ item ( name : string ) {
123+ return < span > { name } </ span >
124+ } ,
125+ } }
126+ > </ HomeChild >
112127 </ div >
113128 )
114129 }
0 commit comments