主按钮 负责 Telegram Mini Apps main button 的💠组件。 安装在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。 Variable : - import { mainButton } from '@telegram-apps/sdk';/ n" d A0 k4 f6 `( ^2 i& Z
- ; S: h0 x7 h$ A: e$ N
- mainButton.mount();& D6 ?" P: L& N Z- e
- mainButton.isMounted(); // true
复制代码Functions : - import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';5 r" m( q/ P Y, e
- 1 P) Y+ b1 u9 F
- mountMainButton();2 j" ^" x+ ~- D9 v, u& E! @
- isMainButtonMounted(); // true
复制代码 0 g5 q- ?* p. K0 f5 N3 b8 R0 r
要卸载,请使用 unmount 方法: Variable : - mainButton.unmount();
8 l6 p5 W, X7 f& j - mainButton.isMounted(); // false
复制代码Functions : - import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
- ^6 e# ?7 V l5 L8 L, { - - u' r+ E- G" R6 A4 m+ }$ s( z
- unmountMainButton();3 ^; ~$ X7 N7 G, v$ O- r8 Y% k
- isMainButtonMounted(); // false
复制代码WARNING 该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。 设置属性要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。 反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, state, textColor 和 text。 Variable : - mainButton.setParams({
- O" f% M, S" G U$ t* g - backgroundColor: '#000000',
. o+ n3 V# h, L1 x9 b# t% w - hasShineEffect: true," H, K" m* x' s& v$ z4 X
- isEnabled: true,$ k; u/ g* o, y/ t$ ], E; Z$ `
- isLoaderVisible: true,2 ~& x4 \3 z, c8 F% p
- isVisible: true,9 F0 R) Q D7 w
- text: 'My text',
( e% [9 S8 U: _( e$ F% g8 j: F - textColor: '#ffffff'; A1 ?8 K p# ?# J) S$ U
- });
% `" d6 ]% h3 z! Q - mainButton.backgroundColor(); // '#000000'1 O' C# N+ v: {) x6 c2 _# c
- mainButton.hasShineEffect(); // true0 h! x) i, i2 I+ {
- mainButton.isEnabled(); // true
; ^( m1 P4 o0 I - mainButton.isLoaderVisible(); // true
7 ^% R: U* p6 b- C U. Q - mainButton.isVisible(); // true
( l+ n ^6 z7 L. l9 \3 C - mainButton.text(); // 'My text'$ k- u" g7 g- U2 _+ E
- mainButton.textColor(); // '#ffffff'
2 w" i3 Q+ g. j
# e0 S4 m& {2 A" n [$ ?' [ Q0 A) [- mainButton.state();6 k; D( g" \) p( g `* r- z" r
- // {4 f' S7 {: Z* h! T% M9 o ]
- // backgroundColor: '#000000',( V* _4 ]! q8 k5 ]. ~. K/ h9 y8 q
- // hasShineEffect: true,
G- t7 `4 ^* x8 X W - // isActive: true,( ]& [# n) V" |( g3 @2 l2 l4 m
- // isLoaderVisible: true,% d+ E. D- p7 ~" P) M( Q' ?
- // isVisible: true, p& ~+ m' e" R
- // text: 'My text',4 F) {# d: c, R0 c; x9 F
- // textColor: '#ffffff'
. b7 {0 p$ W# @: T2 {; h) h - // }
复制代码Functions : - import {
" l/ u) g' k$ @% \5 {: t - setMainButtonParams,2 I4 R! ]2 I) c9 g
- mainButtonBackgroundColor,
O) Y6 [0 x3 X" Q4 M9 ~# _3 q - mainButtonHasShineEffect,
" D0 Q4 ~3 X4 u% Z$ H8 ^- Z - isMainButtonVisible,
$ \9 p' {! |( d& V - isMainButtonEnabled,- j1 h7 S6 K7 K( H1 J
- isMainButtonLoaderVisible,; [3 j+ y# J7 V. \7 H* M
- mainButtonState,: W5 [, x$ z% i; m/ R
- mainButtonTextColor,
0 R" b5 O0 B1 x( Z/ @ } - mainButtonText,+ _" r! ], ?( H0 L# ^1 ]' M
- } from '@telegram-apps/sdk';3 o q+ X, j; Y4 Y6 e
# n$ ?0 d/ w f; Q- setMainButtonParams({. c" `+ Z1 s0 `) `) a! E3 M
- backgroundColor: '#000000',5 p% G# Q: ]4 e- |! ]
- hasShineEffect: true,
; V1 }5 r9 l, n, { - isEnabled: true,
. y" o* X! s9 u- z0 b: z0 V - isLoaderVisible: true,
% z5 O# t6 n' t6 P. _4 \/ ]2 B - isVisible: true,& W/ p1 j C5 Z% d9 D* Q# D: H
- text: 'My text',
r2 u, l! P' x8 B% U* `% A, k - textColor: '#ffffff'
. N- C* f: Z0 T% \/ F8 G; Q; p - });) a! J n: F8 [% T3 w6 ^, O* f
- mainButtonBackgroundColor(); // '#000000'/ n, X/ }" A6 F
- mainButtonHasShineEffect(); // true
+ T9 z( d/ r u( `4 v ] - isMainButtonEnabled(); // true
, o9 `+ _: z) ] - isMainButtonLoaderVisible(); // true. Z3 t# G! t5 L: ]) \
- isMainButtonVisible(); // true: _& e: L) D# e: o+ _9 ?1 P+ ?0 @! J- J
- mainButtonText(); // 'My text'! S* |& F% l% F5 u% S) m
- mainButtonTextColor(); // '#ffffff'
! C* ~$ o) S5 G9 s3 S( J0 o( R
- a! Q3 h5 r6 Q) t- mainButtonState();" F1 I- Q# A3 G w
- // {
$ `& I0 H" Q/ Z$ O) O9 _0 n - // backgroundColor: '#000000',
2 A9 `/ B) n* |- B0 [) x, T# D - // hasShineEffect: true,7 p- ^) M" |1 F" M
- // isActive: true,
0 K k4 f# Q- o - // isLoaderVisible: true,
* d+ f- }, X4 c - // isVisible: true,
% P) `. a( @5 T; _3 o - // text: 'My text',& y/ }+ ^6 q, X; m' U- D( H
- // textColor: '#ffffff'% f! Z1 h* q* c1 S/ K
- // }
复制代码 跟踪点击要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。 Variable : - function listener() {
. W1 M- A2 O7 B& W - console.log('Clicked!');
: F% Q0 E4 K* d3 m0 j1 N# y: [( H4 h) s - }
: u- u; [( U9 p( `5 ~
+ b* {1 k: k" ]% f2 M- const offClick = mainButton.onClick(listener);8 x1 |: g; z. O! {
- offClick();
0 M9 F1 e- H- S$ ]' u. O: y - // or) \$ z+ } u( n- R; G
- mainButton.onClick(listener);) z! b. S; h7 D, Z3 E
- mainButton.offClick(listener);
复制代码Functions : - import {4 L" f( D& m- F
- onMainButtonClick,
0 y2 p) M& h+ p1 w& q' ^. d - offMainButtonClick,* _; r" `, k. O# [2 C
- } from '@telegram-apps/sdk';' V) b7 `. Z6 w" d1 U
) Q& Y9 R; D$ c9 D9 z' Q- g- function listener() {' L% M3 C* {& }) m: Q u5 J
- console.log('Clicked!');
& P& o. A- i/ u* j. ~ - }( _# x; z" s& x# j
- % _- y% x; z$ H1 f5 @' \7 t [
- const offClick = onMainButtonClick(listener);" z0 m$ T2 T; S* R
- offClick();# E) Z5 c k: J6 z' q4 `2 o* i' _0 b
- // or
( w# Y# J3 w$ t. J$ K5 i! d4 @9 O - onMainButtonClick(listener);
9 p* ]7 v+ a/ p) C/ k2 A - offMainButtonClick(listener);
复制代码
7 ?' |7 N3 Q" d, \; @; q7 |4 x% w0 X' v
|