启动 根据该软件包的设计,开发人员可以完全控制其生命周期,包括初始化过程。 这意味着没有预初始化的全局组件 供开发人员使用。 他们必须自己创建组件。 为简化开发人员的工作流程,该软件包包含一些特殊函数,其前缀为 init 字符串。 这些函数会返回一个元组,其中包含初始化组件 的一个实例和一个清理函数,清理函数会移除该 init 函数产生的所有副作用。 下面就是一个例子: - import { initBackButton, initMainButton } from '@telegram-apps/sdk';
2 ~" M1 A+ N/ k3 Q
" P) u' X$ t# c. H6 x- const [mb, cleanupMb] = initMainButton();! K" a6 Z7 J5 E. m( l1 z4 b. J
- const [bb, cleanupBb] = initBackButton();
) a; I7 h/ D2 t8 u& Q8 S) D - 1 S5 W% Q. g( K
- // Clicking the MainButton hides it and shows the BackButton.
4 b Z8 ?- n9 i9 b1 h; N: _; y - mb.on('click', () => {4 F) |7 _* l* L$ q. b2 V$ o5 q8 }, w
- mb.hide();% z* A' o5 _' K q& k+ o
- bb.show();
1 I% {7 W5 T7 ]# y5 G5 x - });4 O1 n% \( ~/ h2 ]. P
' A$ m6 z9 t' ^9 O3 F- // Clicking the BackButton hides it and shows the MainButton.6 w& {- \# v! p
- bb.on('click', () => {; L9 {/ w6 L3 @5 j U6 @" n/ V, z; d
- mb.show();
5 v. t0 L$ `/ a7 G1 q - bb.hide();5 j/ W- C% A. b4 W$ E3 P5 `9 P) M7 ^
- });! p t; {* p' d; V
- 0 o) y& I+ ^9 V3 \; ?
- // Configure the MainButton.7 X8 l* U- }) B
- mb
! p+ S# J% \+ _2 i2 F: N+ E) E - .setBgColor('#ff0000')/ s; q! G1 a3 r( p
- .setTextColor('#ffffff')
" K7 e- W# ~% T; D$ a - .setText('Expand')" k, T8 }" D& s f: _/ R
- .enable()* F! |3 o2 o1 T5 {& i
- .show();# r! |1 [2 [3 P$ O0 n7 u2 Z: Z
- 0 ^1 C, ]- Q6 E' C8 J
- // When we don't need BackButton and MainButton anymore, we can 9 p; R8 f. H+ R% c# m+ X6 c
- // perform a cleanup. After calling a cleanup, the initialized 9 F' [8 ]: R/ l8 X/ Y
- // component will not receive any events.
* I; w3 C! n! @; U3 f - cleanupMb();- q7 r& h+ i+ b; V/ W) a6 O- q( y5 J. s
- cleanupBb();
复制代码INFO 请注意,只要本地没有 相关信息,就不能同步实例化某些组件。 使用每个组件的文档,了解 组件如何初始化。 事件组件实例通过 on 和 off 方法使用常见的事件监听方式。 下面是使用 BackButton 组件的示例 - import { initBackButton } from '@telegram-apps/sdk';" X6 e: u( X; }+ X: f
6 n8 k7 `$ n, ~0 L8 E- const [bb] = initBackButton();
& f. n5 p) @: |6 ]
/ @5 V; y; ]* i, y- // Clicking the BackButton hides it and shows the MainButton.( M+ H3 h2 [. r% b6 H) C! p; I
- bb.on('click', () => {7 D# G' x! T- N3 z2 F) o
- console.log('BackButton clicked.');# Q, ~4 d1 P S! j- m
- });
复制代码 您可以在组件文档中找到支持的事件列表。; \# K) W) u( G! Z
方法支持几乎每个组件都能检查当前 Mini Apps 版本是否支持其方法。 要检查是否支持某些方法,开发人员应使用组件 instance supports() 函数。 例如 - import { BackButton } from '@telegram-apps/sdk';
! _2 r- r: e7 L( J/ M0 L! q - ; K3 d; u* Z: P7 q5 I; Y: N E/ s
- let bb = new BackButton('6.0', ...);
5 @) n4 i. `' p8 J. H5 N" W9 P - bb.supports('show'); // false, e/ y: m! z& m4 `
, f6 d# T. c# G# Y6 b* C- bb = new BackButton('6.3', ...);' U x+ Y3 b& x. V
- bb.supports('hide'); // true
复制代码 某些组件支持附加方法 supportsParam ,可检查 方法参数是否受支持:- import { Utils } from '@telegram-apps/sdk';
" S& z! ?) D) A+ J. x/ H$ m" ^- i- t
" |6 c( o2 I& e: U- let utils = new Utils('6.0', ...);
/ ~; l& K4 v0 b F# p; J# F - utils.supportsParam('openLink.tryInstantView'); // false
9 y) y& ?9 K2 q% l4 b; u8 V" m - # `6 g. T* }/ Q
- utils = new Utils('6.10', ...);( @; R; T% j/ v- r" t; I6 B! t
- utils.supportsParam('openLink.tryInstantView'); // true
复制代码TIP 建议在调用某些组件方法之前使用该功能,只要 能确保开发人员使用该功能。 各组件支持的方法列表见 各组件文档。
7 S: [1 h2 ]; A- S8 h2 f
& v; R1 m! N( x: W2 W- t |