主按钮 负责 Telegram Mini Apps main button 的💠组件。 安装在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。 Variable : - import { mainButton } from '@telegram-apps/sdk';, r8 u2 y) y5 M( d; x
- k* S5 t8 J' J! r/ `' N, f- mainButton.mount();
* Z% o: L. W% y4 V; V$ e- G5 H - mainButton.isMounted(); // true
复制代码Functions : - import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
4 D9 {' ~2 s& h! l: L6 L# O
, q' J+ O5 T1 L4 n5 T1 n- mountMainButton();
5 g* y2 q% {6 s; B: |" o - isMainButtonMounted(); // true
复制代码 . ]9 U i' ]) ^; v! l
要卸载,请使用 unmount 方法: Variable : - mainButton.unmount();
4 `/ [) X9 G4 \* x0 g - mainButton.isMounted(); // false
复制代码Functions : - import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
: Y' i" E4 r3 q - ) }; j7 c Y6 Y2 @
- unmountMainButton();3 x( Z2 \2 ]3 o% o4 D5 O6 h
- isMainButtonMounted(); // false
复制代码WARNING 该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。 设置属性要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。 反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, state, textColor 和 text。 Variable : - mainButton.setParams({) D! `. L! S. @% X, [4 A$ e
- backgroundColor: '#000000',9 e: t2 t& r1 {
- hasShineEffect: true,
! A# s9 F N7 l4 e0 G: j H! A - isEnabled: true,- c1 v' b$ }% g- Y: U6 Q. e
- isLoaderVisible: true,
& ^- u3 s2 j- p- r - isVisible: true,
/ T ~; T# E' e7 o6 W1 b - text: 'My text',
+ v: \1 t# N" B9 V( O" H - textColor: '#ffffff'- W0 g3 c' o0 u1 ?6 G
- });, D$ g% l! N+ Y" {. ?8 D5 B
- mainButton.backgroundColor(); // '#000000'
+ C, ]7 \& b% ~ - mainButton.hasShineEffect(); // true
) e: X0 S6 }' k# e9 \# L. J( [ - mainButton.isEnabled(); // true
" z8 V$ k, j% D! n' c% n) q1 | ? - mainButton.isLoaderVisible(); // true
1 x) a; T. k0 D7 p+ F6 V7 j, S6 I - mainButton.isVisible(); // true% q) @2 u1 v' m' H9 ]% |' z! ~5 z4 A
- mainButton.text(); // 'My text'
' [6 X4 o3 Z1 I3 `3 ? - mainButton.textColor(); // '#ffffff': i4 X; P9 |% h3 ^6 l
- 4 @3 a' I2 l& Y. ]
- mainButton.state();9 l+ t( q x# }6 W4 I) ?
- // {5 `1 i$ b# w/ L6 U) Z
- // backgroundColor: '#000000',& j4 D" e2 ^" e- Z8 ?
- // hasShineEffect: true,. [9 `* \. g8 m9 Q
- // isActive: true,8 N7 _' ]) z$ ?. c
- // isLoaderVisible: true,3 f' J2 Q S/ q2 n* ?0 r2 @
- // isVisible: true,% l9 P/ r: P, G, m
- // text: 'My text',
6 t$ _: v; v2 B% B" G - // textColor: '#ffffff'
0 [! M X$ O7 X6 Q' Z4 E - // }
复制代码Functions : - import {4 a, G j- D p( e8 Y R
- setMainButtonParams,
. i" B" Y( ~: C# e - mainButtonBackgroundColor,
+ ~$ D' W3 t: T& p" x( |' G - mainButtonHasShineEffect,
# ]9 Z" ?" f# E! J, ~ - isMainButtonVisible,' I* h' ~# c! r% E) w, q
- isMainButtonEnabled,
! D( z& i( n2 \. \5 v7 O3 m3 O0 C1 @" C - isMainButtonLoaderVisible,2 Y7 c r: R1 C3 X
- mainButtonState,
. H* }+ ~7 V2 e1 M. j& K) R - mainButtonTextColor,1 \8 j( b) G; q' ~ ?
- mainButtonText,
3 Q* }5 w1 C$ A# d0 C - } from '@telegram-apps/sdk';% v3 C- @. i/ R2 O& a: X8 [. o3 E
. _5 L; D; J( F5 `$ F0 x. q- setMainButtonParams({
- D2 l. z3 ?% g% F - backgroundColor: '#000000',
# J! Z+ d- I7 T% ?% b$ Y! N B, r9 f1 N - hasShineEffect: true,
9 ]* d: n0 y4 d- v - isEnabled: true,
- R, n1 r6 h8 t9 x, Q1 [; e - isLoaderVisible: true,
9 D, c2 m- F# U3 m - isVisible: true,
" \! i0 c% l# }" L - text: 'My text',
! N$ }, W9 H3 H4 o+ D O - textColor: '#ffffff' h, R' C0 a* u
- });2 A3 j+ L; Y$ f5 d, P
- mainButtonBackgroundColor(); // '#000000': C0 x: l' p8 S6 @8 a
- mainButtonHasShineEffect(); // true; h9 ]% j6 @7 ~& e9 N, Y
- isMainButtonEnabled(); // true
' j4 V v$ o% ]7 I; Z' ~% x3 l1 a- _ - isMainButtonLoaderVisible(); // true
' C* m, n- `+ E' X5 h3 q# u - isMainButtonVisible(); // true K8 {6 p' W* w( y* B- S
- mainButtonText(); // 'My text'3 V N0 J3 C! c! J/ ?
- mainButtonTextColor(); // '#ffffff'! m( F3 h% u( E
- 0 A% P6 y) X) R) M9 A4 d
- mainButtonState();+ P9 z: t9 N, ]( z6 T' e5 z
- // {
) Y3 Y, {. x, p5 ~ - // backgroundColor: '#000000',
; Z" O! @- L+ y. s9 U4 f - // hasShineEffect: true,
4 |4 P, A0 e% l+ Y$ y$ C8 J: | - // isActive: true,
9 \. c: K2 T3 d+ ?% o- E - // isLoaderVisible: true,
* N5 h& @) ?1 L$ Y7 B/ {# P8 Y - // isVisible: true,
/ d5 _0 O! O( A& T0 ]; E - // text: 'My text',
: o. L0 c/ t& x& h$ H1 D - // textColor: '#ffffff'! o7 s }4 U+ t1 a5 Y. e" D
- // }
复制代码 跟踪点击要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。 Variable : - function listener() {0 q6 X) ?9 S( N7 q! `
- console.log('Clicked!');
" _4 I" x) N. L& e8 b4 h5 W6 x( L: I - } n3 [1 l5 }& ^7 `4 h: f
* ?% P {: W: @7 ^: r6 E- const offClick = mainButton.onClick(listener); d; A4 V8 \' Q D; O7 B, T$ W
- offClick();/ J' f( `2 x; c" b9 N. w5 k3 s! \6 V
- // or
! d$ ?6 ^* m8 @- j - mainButton.onClick(listener);
* X' c' t1 X7 T- y+ k - mainButton.offClick(listener);
复制代码Functions : - import {
( R* x3 W9 W+ ^6 W. n6 @ - onMainButtonClick,( f& n j2 Z) u# u% J: F4 r
- offMainButtonClick,
+ ]7 n. A5 v! Y5 z0 q - } from '@telegram-apps/sdk';3 Z- w5 `* u. C
0 l5 b7 e4 W; K5 k6 T' C- function listener() {# L/ e. m& Y- F
- console.log('Clicked!');" t4 S$ j. a& Z. q# [+ B
- }
/ I$ d, k# I' f# \ - ) j1 h+ f& n. k3 }3 l9 e' M
- const offClick = onMainButtonClick(listener); F$ P; t0 @; Y
- offClick();
9 Z3 L4 }& }" p) g - // or
! S D# d- k- y3 n - onMainButtonClick(listener);
, U/ s" y, e* O, a8 j1 O: Y - offMainButtonClick(listener);
复制代码
6 L& p# S' y* @1 r6 Q$ c: ]1 R" q3 A t4 ?4 e) U% D
|