主按钮 负责 Telegram Mini Apps main button 的💠组件。 安装在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。 Variable : - import { mainButton } from '@telegram-apps/sdk';) p: i$ [4 \% E% U1 N% M/ g! f3 T1 t
- 2 Q# C7 I0 m/ N9 n, j
- mainButton.mount();
: A7 }$ F! u# w) G9 X \ - mainButton.isMounted(); // true
复制代码Functions : - import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';% G' s4 |9 I, r# ^. l9 e( @8 R( l
- ) i ]) i+ U+ ?$ l4 ~4 i/ ?( M/ Z3 _! e
- mountMainButton();
' A0 U. o6 j6 |) T: i7 r: I! W - isMainButtonMounted(); // true
复制代码 4 Y! W8 L! b" C; R- @$ }
要卸载,请使用 unmount 方法: Variable : - mainButton.unmount();
6 ^* o* a' Y8 V/ ?& a9 \4 t7 Y - mainButton.isMounted(); // false
复制代码Functions : - import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
( z/ B1 q8 A" U0 m) ~4 d& p - * @/ p1 k! c, K) y9 t: c4 q% l
- unmountMainButton();
G5 l. e% l. `- d0 s0 T - isMainButtonMounted(); // false
复制代码WARNING 该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。 设置属性要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。 反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, state, textColor 和 text。 Variable : - mainButton.setParams({
& e5 w. a9 t" u5 l7 y4 S - backgroundColor: '#000000',
6 Z/ B+ X5 j* |. H7 f: v - hasShineEffect: true,; A' I# H7 l6 s# B; f
- isEnabled: true,
" h& p9 k' k8 w4 `5 w: S7 p, y7 [6 \ - isLoaderVisible: true,8 B. _$ i( _: M6 P
- isVisible: true,+ ]4 z; z) `8 d4 v/ D7 j6 c
- text: 'My text',
) B/ y) c: I2 P) q) F' W - textColor: '#ffffff'; r1 h- e" ?) N% ?, s1 \' u2 ?
- });
0 a. z4 d, n$ t- q - mainButton.backgroundColor(); // '#000000'
% k9 i3 I2 o9 Y! a7 K - mainButton.hasShineEffect(); // true. b5 V M" l: i" c6 L7 |, c% H
- mainButton.isEnabled(); // true. H) j) l8 F4 [8 u( D% b. v/ x; z0 {
- mainButton.isLoaderVisible(); // true
: w! s/ c7 T/ t( t - mainButton.isVisible(); // true
( d/ [/ R+ E& v, R, }: t - mainButton.text(); // 'My text'; A0 I- m/ u/ B9 k' @* u
- mainButton.textColor(); // '#ffffff'
) B! ] L9 ]6 {" d3 L$ [( C1 x - " z4 d0 e6 u6 k& g( d2 M0 g
- mainButton.state();
0 s5 i4 _: q. T8 Y2 x3 G$ F* j3 l - // {0 U8 u6 Q+ b% d2 a
- // backgroundColor: '#000000',/ h. ~; o, a8 x. T) r
- // hasShineEffect: true,
3 [8 K8 C$ l0 q& j4 z+ w - // isActive: true,5 y) c* F/ u# p% x( m% a5 H
- // isLoaderVisible: true,8 x/ b! _- p; w/ R A+ A0 C3 B
- // isVisible: true,
4 L% m4 b9 F+ k8 E+ M; }# n - // text: 'My text',9 F" l$ X) S% D$ j# @# V2 H$ z
- // textColor: '#ffffff'- Q6 M6 z" w5 E' L
- // }
复制代码Functions : - import {
! H9 |. w0 |% V1 q: S+ B' G - setMainButtonParams,
. A' S7 m( E+ } - mainButtonBackgroundColor,4 ~" ^9 L6 l& ~0 \" P* r8 I
- mainButtonHasShineEffect,; e M( `) [8 e/ \7 r
- isMainButtonVisible,
" g% K5 M( q9 f K - isMainButtonEnabled,1 e! l( l& L! d5 b, J! F
- isMainButtonLoaderVisible,9 b ^; G7 J q0 K. Z! F6 D! U
- mainButtonState,
; F9 C+ ]4 d/ z. c) ? - mainButtonTextColor,
8 N6 j4 o4 G+ p- Y+ p - mainButtonText,; `* ^( E6 g2 a& t5 e
- } from '@telegram-apps/sdk';
9 L( y o4 r) C- w( K7 F - M& Y6 d, |, j
- setMainButtonParams({! R) {8 h( ~& C4 _7 d1 ]7 `
- backgroundColor: '#000000',
+ D+ ?2 w* [, d1 v7 ?( Q - hasShineEffect: true,
( ^+ N9 i- r) g3 q - isEnabled: true,
$ J7 e0 M3 F7 S' i& n - isLoaderVisible: true,2 {5 N" Z7 m# [" H8 V/ ~ P& O% J
- isVisible: true,$ P/ d3 c; O4 `! h9 z8 m9 n. U5 |
- text: 'My text',6 s. v5 M' I2 _6 y! b# ~: c/ r
- textColor: '#ffffff') Q4 E% Y5 m( b, g" y
- });
. l4 {$ U1 M/ v9 a& ?4 F6 D. ?, @ - mainButtonBackgroundColor(); // '#000000'2 M" P# t# D8 r' h }1 R
- mainButtonHasShineEffect(); // true- a* Z. t7 I2 f/ g
- isMainButtonEnabled(); // true! M3 y: Y! V% u: K% d
- isMainButtonLoaderVisible(); // true+ |- U, E, k# p+ \$ b! w, X0 d" X
- isMainButtonVisible(); // true
6 i: P5 Q# p- E( N% w1 t3 n+ J# @ - mainButtonText(); // 'My text'5 X4 x; R; P+ m2 n' \6 r+ \
- mainButtonTextColor(); // '#ffffff'
- I) ]9 v, @4 R8 [9 r7 o
. s8 B" t8 k0 U8 i/ i3 Y- mainButtonState();( X& F3 k2 L4 M5 x+ q7 q# N
- // {+ m: P! b( v" S
- // backgroundColor: '#000000',
' n7 T- i3 |" `; k - // hasShineEffect: true,
, R, `2 ?. p% E7 B9 i - // isActive: true,
2 x% ~6 I T# f& P2 {1 x - // isLoaderVisible: true,
; S$ W: {2 O+ G - // isVisible: true,
) i7 s- A) {8 D - // text: 'My text',) Y3 W* n4 h/ d% ^
- // textColor: '#ffffff'
5 @9 o4 o+ }8 r - // }
复制代码 跟踪点击要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。 Variable : - function listener() {
$ h9 o3 `5 u0 O4 } - console.log('Clicked!');
4 e0 O4 r% p8 C( v' a) Q - }
- Z- P+ j1 o7 W' P( A - + _+ h1 |- L" P( A z: @7 x
- const offClick = mainButton.onClick(listener);( e( C! g' S- M" X% D9 L: {
- offClick();
5 _+ o H% ?" g4 G - // or
9 g3 B5 M) D5 t! p, z- m5 l% v; a* X - mainButton.onClick(listener);
: W" ]) ?& q+ Y1 f6 ?: U+ h, s - mainButton.offClick(listener);
复制代码Functions : - import {9 V( g' w# m0 h9 t% f( m* g
- onMainButtonClick,
6 S) y( c0 q$ r8 h - offMainButtonClick,# z7 ?2 N8 c4 D- L
- } from '@telegram-apps/sdk';
3 Y4 z1 T& _! |1 R# Q2 h) a/ g
2 U! L2 t& O" p. V- function listener() {
- d9 |: @0 P2 l9 S0 E - console.log('Clicked!');; F0 m7 S0 y' ~
- }% Q6 @5 N1 i5 N2 ~9 |1 s
4 [& x& r- L$ N- const offClick = onMainButtonClick(listener);1 R5 m# }& P, N9 Z: T$ W1 x9 R0 \
- offClick();
. V5 i# v6 u* z! j; y# |$ {( m% H - // or9 ~" M& f9 b5 U7 T( Q
- onMainButtonClick(listener);
+ G; u$ f5 t, d { - offMainButtonClick(listener);
复制代码 & T K7 G4 a- H6 M6 H
4 j& C% \- r6 M2 Q( y6 E
|