启动 根据该软件包的设计,开发人员可以完全控制其生命周期,包括初始化过程。 这意味着没有预初始化的全局组件 供开发人员使用。 他们必须自己创建组件。 为简化开发人员的工作流程,该软件包包含一些特殊函数,其前缀为 init 字符串。 这些函数会返回一个元组,其中包含初始化组件 的一个实例和一个清理函数,清理函数会移除该 init 函数产生的所有副作用。 下面就是一个例子: - import { initBackButton, initMainButton } from '@telegram-apps/sdk';4 t9 d F. ?; q2 p+ C
- ( B$ U9 U1 ~2 Q+ b0 Y* \' i
- const [mb, cleanupMb] = initMainButton();
8 L: e- v& }& ^1 u. |- C( t: l, D: W - const [bb, cleanupBb] = initBackButton();
6 R- _/ A$ P% E% [7 d& s
2 h3 r0 h( S8 s: M/ b) o( t- E- U- // Clicking the MainButton hides it and shows the BackButton.
7 D, ]0 z' D; R; V6 k - mb.on('click', () => {% L5 d6 U+ ?$ @) ~+ u8 [6 r6 _
- mb.hide();) W5 _( `# y& v2 i
- bb.show();
6 x4 t* m; D0 x" S( d - });" A6 K# A6 Z, Y+ H( i% X
- % s1 | h: o! y1 R2 O: [, K
- // Clicking the BackButton hides it and shows the MainButton.
0 h* R0 b: K% E8 v: m; B - bb.on('click', () => {
; |; Q L: q$ H0 ` - mb.show();- j! T# y; r$ H' _
- bb.hide();, \0 @7 W! ]1 Z! T5 ^/ D/ [2 J
- });3 s4 W5 {2 |% Q
- 4 Y3 _# B3 P* x' v: i
- // Configure the MainButton.
/ `* L& X+ @6 P/ q' q( F - mb
# G$ L' E R& F" f3 E% v - .setBgColor('#ff0000'). a0 x! U- B1 W1 E; r s) Y6 x
- .setTextColor('#ffffff')7 K" ~: `- V: R
- .setText('Expand')
( t3 l( R8 Q' x2 F - .enable()/ S+ x4 U8 Y7 J- C
- .show();
* P! t3 {( c% ~: u3 u+ G
' p" e+ ?% U" J: d6 j- // When we don't need BackButton and MainButton anymore, we can
0 o) R# e7 K! X+ _, @, ?+ b: _ - // perform a cleanup. After calling a cleanup, the initialized
5 D! ?% Z' W% t/ y- V5 g - // component will not receive any events.! A0 k/ b3 P& t
- cleanupMb();
/ j; {% q/ b* a- ^6 S; w1 l - cleanupBb();
复制代码INFO 请注意,只要本地没有 相关信息,就不能同步实例化某些组件。 使用每个组件的文档,了解 组件如何初始化。 事件组件实例通过 on 和 off 方法使用常见的事件监听方式。 下面是使用 BackButton 组件的示例 - import { initBackButton } from '@telegram-apps/sdk';
# d: e4 j% i+ m6 L( d" p
+ z$ U1 J* j# |# `1 t. c/ ]- const [bb] = initBackButton();( H; ^3 U+ c/ k0 H7 M. \
- 2 J0 y* R d( [
- // Clicking the BackButton hides it and shows the MainButton.
6 J( N+ B) o6 p/ _! L2 p! z5 W - bb.on('click', () => {
8 @( {$ q" j; Q C - console.log('BackButton clicked.');$ p, Q1 w' g8 c0 C, Y& E
- });
复制代码 您可以在组件文档中找到支持的事件列表。. @, T' j, Q$ x; R4 j+ x) G, @
方法支持几乎每个组件都能检查当前 Mini Apps 版本是否支持其方法。 要检查是否支持某些方法,开发人员应使用组件 instance supports() 函数。 例如 - import { BackButton } from '@telegram-apps/sdk';
& T! U2 o d/ P. N - - z3 G& ^8 Z6 _' S: ~. D: G" ?
- let bb = new BackButton('6.0', ...);
. L5 J/ }$ d, c: N/ Z - bb.supports('show'); // false
+ K7 J% g% h% o8 |) p/ q1 n D* a. a
" _& I. {4 S; U y- bb = new BackButton('6.3', ...); S3 I6 u* [- G: v( c$ f
- bb.supports('hide'); // true
复制代码 某些组件支持附加方法 supportsParam ,可检查 方法参数是否受支持:- import { Utils } from '@telegram-apps/sdk';! [3 Z: r" t7 n, E: r
- 9 D+ V e+ Q+ T9 O
- let utils = new Utils('6.0', ...);; m& P$ E( Y2 X; a- ~" ?3 {. W' P
- utils.supportsParam('openLink.tryInstantView'); // false2 G* v$ h6 J# q+ {' f* V3 ~
3 O a& \/ u( O" {" K2 o# b% ]- utils = new Utils('6.10', ...);* P0 D4 y- V- q, F' `# I
- utils.supportsParam('openLink.tryInstantView'); // true
复制代码TIP 建议在调用某些组件方法之前使用该功能,只要 能确保开发人员使用该功能。 各组件支持的方法列表见 各组件文档。 5 X3 q' ^5 ?6 D! _/ B1 l
) Q& B% d4 `- h
|