Skip to content

Commit da452fa

Browse files
committed
a
1 parent 6612abe commit da452fa

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

readme.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Read me
2+
3+
This repo is not released yet. Welcome to suggest and contribute. Message me on github.
4+
5+
# Example
6+
7+
```typescript
8+
import { Component ,Base} from "ThisRepo";
9+
@Component
10+
export default class MyComponent extends Base{
11+
prop = "p";//a property
12+
meth() {//a method
13+
this.$forceUpdate()//call vue api
14+
}
15+
mounted(){//vue lifecycle hook
16+
console.log('foo')
17+
}
18+
}
19+
```
20+
21+
is equal to
22+
23+
```typescript
24+
import { defineComponent} from "vue";
25+
export default defineComponent({
26+
data(){
27+
return {
28+
prop:'p'
29+
}
30+
},
31+
methods:{
32+
meth(){
33+
this.$forceUpdate()
34+
}
35+
},
36+
mounted() {
37+
console.log('foo')
38+
}
39+
})
40+
```

src/component.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import { defineComponent } from 'vue';
22
import { obtainSlot } from './utils';
3-
function makeObject(names:string[],obj:any){
3+
4+
const LifecycleNames = [
5+
"beforeCreate",
6+
"created",
7+
"beforeMount",
8+
"mounted",
9+
"beforeUpdate",
10+
"updated",
11+
"activated",
12+
"deactivated",
13+
"beforeDestroy",
14+
"beforeUnmount",
15+
"destroyed",
16+
"unmounted",
17+
"renderTracked",
18+
"renderTriggered",
19+
"errorCaptured"
20+
]
21+
22+
23+
function makeObject(names: string[], obj: any) {
424
return names.reduce<Record<string, any>>((pv, cv) => {
525
pv[cv] = obj[cv]
626
return pv
@@ -10,11 +30,17 @@ export function Component(cons: { new(): any, prototype: any }) {
1030
const slot = obtainSlot(cons.prototype)
1131
const sample = new cons
1232
const proto = cons.prototype
13-
const methodNames = Object.getOwnPropertyNames(cons.prototype).filter(name => {
33+
const LifecycleFunctions:Record<string,Function>={}
34+
const methodNames = Object.getOwnPropertyNames(proto).filter(name => {
1435
if (name === 'constructor') {
1536
return false
1637
}
1738
if (typeof proto[name] === 'function') {
39+
if(LifecycleNames.includes(name)){
40+
LifecycleFunctions[name]=proto[name]
41+
return false
42+
}
43+
1844
return true
1945
}
2046
})
@@ -23,9 +49,10 @@ export function Component(cons: { new(): any, prototype: any }) {
2349

2450
const def = defineComponent({
2551
data() {
26-
return makeObject(dataNames,sample)
52+
return makeObject(dataNames, sample)
2753
},
28-
methods: makeObject(methodNames,proto)
54+
methods: makeObject(methodNames, proto),
55+
...LifecycleFunctions
2956
})
3057
return def as any
3158

src/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22
export { Component } from './component'
3-
import { ComponentInternalInstance, ComponentPublicInstance, Slots, nextTick, WatchOptions, WatchStopHandle } from 'vue'
3+
import {
4+
ComponentInternalInstance,
5+
ComponentPublicInstance,
6+
Slots,
7+
nextTick,
8+
WatchOptions,
9+
WatchStopHandle,
10+
11+
} from 'vue'
412

513
// export declare class Base {
614

@@ -38,4 +46,22 @@ export class Base {
3846
$nextTick!: typeof nextTick;
3947
$watch!: { (source: string | Function, cb: Function, options?: WatchOptions): WatchStopHandle };
4048

49+
50+
beforeCreate?(): void;
51+
created?(): void;
52+
beforeMount?(): void;
53+
mounted?():void;
54+
beforeUpdate?(): void;
55+
updated?(): void;
56+
activated?(): void;
57+
deactivated?(): void;
58+
/** @deprecated use `beforeUnmount` instead */
59+
beforeDestroy?(): void;
60+
beforeUnmount?(): void;
61+
/** @deprecated use `unmounted` instead */
62+
destroyed?(): void;
63+
unmounted?(): void;
64+
renderTracked?(e: any): void;
65+
renderTriggered?(e: any): void;
66+
errorCaptured?(err: any, instance: ComponentPublicInstance | null, info: string): boolean | void;
4167
}

0 commit comments

Comments
 (0)