UNPKG

2.15 kBTypeScriptView Raw
1declare namespace tinyapp {
2 interface IComponentLifeCycleMethods<D, P> {
3 /**
4 * 组件生命周期函数,组件创建时触发
5 */
6 onInit?(): void;
7
8 /**
9 * 组件生命周期函数,组件创建时和更新前触发
10 *
11 * @param nextProps 接收到的 props 数据
12 */
13 deriveDataFromProps?(nextProps: Partial<P>): void;
14
15 /**
16 * 组件生命周期函数,组件创建完毕时触发
17 */
18 didMount?(): void;
19
20 /**
21 * 组件生命周期函数,组件更新完毕时触发
22 */
23 didUpdate?(prevProps: Partial<P>, prevData: Partial<D>): void;
24
25 /**
26 * 组件生命周期函数,组件删除时触发
27 */
28 didUnmount?(): void;
29 }
30
31 interface IComponentMethods {
32 [name: string]: (...args: any[]) => void;
33 }
34
35 interface IComponentInstance<P, D> extends Record<string, any> {
36 /**
37 * 组件内部状态
38 */
39 readonly data: D;
40
41 /**
42 * 传入组件的属性
43 */
44 readonly props: P;
45
46 /**
47 * 设置data触发视图渲染
48 */
49 setData: SetDataMethod<D>;
50
51 /**
52 * 组件所属页面实例
53 */
54 readonly $page: IPageInstance<any>;
55
56 /**
57 * 组件 id,可直接在组件 axml 中渲染值
58 */
59 readonly $id: number;
60
61 /**
62 * 组件路径
63 */
64 readonly is: string;
65
66 /**
67 * 设置data触发视图渲染
68 */
69 $spliceData: (operations: { [k: string]: [number, number, ...any[]] }) => void;
70 }
71
72 type ComponentOptions<
73 P extends Record<string, any> = Record<string, any>,
74 D = any,
75 M extends IComponentMethods = IComponentMethods,
76 > = IComponentLifeCycleMethods<D, P>
77 & {
78 /**
79 * 组件间代码复用机制
80 */
81 mixins?: Array<ComponentOptions<any, any, any>>;
82
83 /**
84 * 组件内部状态
85 */
86 data?: D;
87
88 /**
89 * 为外部传入的数据设置默认值
90 */
91 props?: P;
92
93 /**
94 * 组件的方法,可以是事件响应函数或任意的自定义方法
95 */
96 methods?: M & ThisType<IComponentInstance<P, D> & M>;
97 }
98 & ThisType<IComponentInstance<P, D> & M>;
99}