主按钮 负责 Telegram Mini Apps main button 的💠组件。 安装在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。 Variable : - import { mainButton } from '@telegram-apps/sdk';
5 h \9 D) E3 V B$ H - " K, ?7 ~7 v: [0 D* g! _" w
- mainButton.mount();
1 A0 k! C6 T! G# o% l - mainButton.isMounted(); // true
复制代码Functions : - import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';" `1 s2 y/ P; S( c2 o# M5 E
- 1 t6 c! X. w K- R8 J
- mountMainButton();
% D* q [0 d/ q$ R$ J - isMainButtonMounted(); // true
复制代码 ) W0 u# E. _3 d7 k$ H4 G
要卸载,请使用 unmount 方法: Variable : - mainButton.unmount(); 8 M0 A: i; r) f) Q# D- ~
- mainButton.isMounted(); // false
复制代码Functions : - import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
/ G6 P6 {6 ` C. J; }8 }+ Q- k/ o
/ b" H( N" H: i- w1 E- unmountMainButton();
) ?" p: s' B& a- f0 W6 p% r - isMainButtonMounted(); // false
复制代码WARNING 该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。 设置属性要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。 反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, state, textColor 和 text。 Variable : - mainButton.setParams({
' L/ X# N( { f - backgroundColor: '#000000',
6 y% T5 S( L5 E) j3 ` - hasShineEffect: true,
8 ]9 D7 O$ K# e' {6 x - isEnabled: true,
. s. f; R5 w2 X0 c( }$ k5 p - isLoaderVisible: true,! A1 M. D6 ~0 P- @/ }4 _( w
- isVisible: true,
& {8 c0 U# c) b1 B2 e+ H3 F4 {$ K9 p - text: 'My text',/ O- m& X9 Q; \* Q8 E7 y
- textColor: '#ffffff'
# l4 B* z* N. [3 E5 Q8 m - });
7 L' ]9 L+ o& X# M" c# `5 { - mainButton.backgroundColor(); // '#000000'
, {. Y' G3 o0 B: K6 F - mainButton.hasShineEffect(); // true
7 ^; S2 G7 k% n% G6 Q/ g- I, Z; _, S - mainButton.isEnabled(); // true" C3 I) Z2 k& R# k" Z j. n
- mainButton.isLoaderVisible(); // true
7 J q: w; M9 ?0 }- {2 u - mainButton.isVisible(); // true
* f$ ^2 D& ?5 { - mainButton.text(); // 'My text'6 L1 u6 C9 V9 C4 S% \
- mainButton.textColor(); // '#ffffff', V$ O: w2 p8 z) R" {
) h# m# [2 U' C+ M- mainButton.state();
' L% ?+ }5 M9 ?+ l! }) I8 l - // {8 O k' j% F: `3 z. Z& T* N
- // backgroundColor: '#000000',2 z3 L8 V6 |7 z* [$ H
- // hasShineEffect: true,: n! ~( ~9 i: s% v: i) B& X' x
- // isActive: true,1 b [2 @2 |; `8 ?
- // isLoaderVisible: true,
: E$ t/ O. [! w0 z+ Y& X% r# S - // isVisible: true,
' ~3 a: O' [5 S - // text: 'My text',
y/ m$ {& z: k7 g( F6 L+ V - // textColor: '#ffffff'
* }% l& K- o# {$ o - // }
复制代码Functions : - import {
: B& j& e' ]' l; {5 S - setMainButtonParams,! u7 d/ Q( Z* q0 A
- mainButtonBackgroundColor,
5 F9 B/ r: C" r - mainButtonHasShineEffect,
* ]0 z9 S' D5 L6 v - isMainButtonVisible,+ D: H* z$ J8 q+ o" l8 z
- isMainButtonEnabled,
: L; v+ j9 F0 d3 Y' x1 ^, u4 H5 \ - isMainButtonLoaderVisible,
! c, x, U4 N$ E. C5 R - mainButtonState,
7 A) N7 b/ Q5 B0 y3 m - mainButtonTextColor,
# V- I! r6 d/ l* D7 b1 T8 I - mainButtonText,
, J% v- w' p7 {& t; ^ - } from '@telegram-apps/sdk';
# B/ t5 Y* ~- R* w" b - 4 I8 {0 s! A1 n
- setMainButtonParams({" P* |+ x7 z" Q, y) g' E* a
- backgroundColor: '#000000',5 R! T2 V1 u: F3 U7 q7 A3 T
- hasShineEffect: true,
, [; Q' _) h$ X - isEnabled: true,1 J, M* q4 X+ F7 ~4 Z, z
- isLoaderVisible: true,
, ?4 a! X: f$ H9 l) U - isVisible: true,5 Y) i: Q% \/ g- f' _) u: Y4 s
- text: 'My text',
* A( y6 h4 O. v; M: l& ~+ O7 t - textColor: '#ffffff'
- t* C2 I! K# I9 p+ z' r - });
' l2 F1 O1 F* A k, |" P1 \ - mainButtonBackgroundColor(); // '#000000'
2 e% c, l: }: u' ]) b6 ~ - mainButtonHasShineEffect(); // true9 z/ M8 D5 M9 W; ]# N9 b2 m3 R
- isMainButtonEnabled(); // true) J6 J7 w5 J9 J/ K
- isMainButtonLoaderVisible(); // true
, N; \. g! s5 a* h0 T. ?8 B" a - isMainButtonVisible(); // true" N. s: i% n7 x, ]4 C
- mainButtonText(); // 'My text'
3 i* S2 A$ t8 |2 b - mainButtonTextColor(); // '#ffffff'5 [9 v# [3 |7 P6 J/ \5 d
4 _! p, V: L# e U2 [- mainButtonState();' E4 u( r1 V4 B0 s: _+ A# S9 g) Z
- // {# v9 D7 S, x8 k# P( D& ]3 ^
- // backgroundColor: '#000000',2 G8 }" M6 r! ~* ~
- // hasShineEffect: true,$ u$ a) }6 a1 F3 B) b
- // isActive: true,3 [5 Z O3 j- C- o7 P+ G( q
- // isLoaderVisible: true,
+ v, f! O( g; d, H8 C8 ] - // isVisible: true,
4 Z5 P* l" ~/ F' E- d% v - // text: 'My text',
, P, {# G5 _# O, C* M - // textColor: '#ffffff'
' ]( r, O6 B( a* }) u5 i - // }
复制代码 跟踪点击要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。 Variable : - function listener() {
c, a! U/ E, v* o - console.log('Clicked!');) S) O6 K; Q8 x7 B9 X
- }3 V4 S! d: w3 w
z1 d( i; t7 E" \/ t+ x- const offClick = mainButton.onClick(listener);6 Z# L& ~2 n, {$ O2 e
- offClick();, L% g( j+ P/ {: _& j
- // or
( T5 L/ y i$ d% {" X - mainButton.onClick(listener);. r( ?9 c) M( ~0 e0 @
- mainButton.offClick(listener);
复制代码Functions : - import {+ h$ V/ p; Z. }% U+ Y5 m, [
- onMainButtonClick,: L" ~% B8 ?4 q9 R; N( d# t/ b1 b
- offMainButtonClick,: x# n( L0 b( a; U2 A
- } from '@telegram-apps/sdk';9 S9 {; i2 `4 a" e6 B/ v& _
- 6 x( t& f7 Q( P1 @
- function listener() {
: I0 w, m5 r- {, R: C - console.log('Clicked!');
8 e9 \9 U* N7 q* Z: u) m - }
2 A( ^; c& v! E4 ~ - % @5 a- A5 ?7 [5 s! y
- const offClick = onMainButtonClick(listener);
6 z* Y- [6 t+ T* o$ u8 ~6 ?6 I - offClick();: P+ l. K" a6 A$ y: n3 H
- // or
# ?8 V1 F" o. n* _& P - onMainButtonClick(listener);
$ s$ F& E8 Q: D' c2 A: F( J& w - offMainButtonClick(listener);
复制代码
5 @8 P& f' Q/ f+ R5 d, L6 g1 [5 ^9 z! }+ j2 }# y
|