主按钮 负责 Telegram Mini Apps main button 的💠组件。 安装在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。 Variable : - import { mainButton } from '@telegram-apps/sdk';& w# N# ]' ?$ H% w. z5 n
- ; E }* e; H! b9 {- e( a
- mainButton.mount();( [$ f' s# A1 b
- mainButton.isMounted(); // true
复制代码Functions : - import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
& i8 \: I }4 @; ?' M7 r) r, {
% a Z, J/ K6 m1 \ |1 M- mountMainButton();- ?) ]* _& Q7 L6 V
- isMainButtonMounted(); // true
复制代码 % ^3 p4 t% `& W# }4 e+ H
要卸载,请使用 unmount 方法: Variable : - mainButton.unmount();
6 J" k) _& }5 Q1 m8 |0 U( H - mainButton.isMounted(); // false
复制代码Functions : - import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
+ d( I0 x- n4 m2 u
2 k% H% K+ b d2 T- unmountMainButton();9 S2 G9 A7 h9 Z7 l
- isMainButtonMounted(); // false
复制代码WARNING 该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。 设置属性要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。 反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, state, textColor 和 text。 Variable : - mainButton.setParams({, C" x* ~3 }7 W
- backgroundColor: '#000000',
# f' o5 M0 v- `, B+ c# H& f - hasShineEffect: true,
( r5 c& T I: ]0 x& L - isEnabled: true,
9 t# D& \9 T4 J- @ - isLoaderVisible: true,
5 W) d/ w b/ l, | - isVisible: true,3 U2 m a; |; H! M/ n2 n. X& O
- text: 'My text',, J, a$ Q* r. `/ ?0 Q
- textColor: '#ffffff'6 R( }; L; ~5 g& T3 z; h
- });0 f/ Q- I# N/ b5 Z) ^5 X* v
- mainButton.backgroundColor(); // '#000000'
' L6 M" U# B: r: R( j - mainButton.hasShineEffect(); // true1 E5 _0 F$ H; z/ Q
- mainButton.isEnabled(); // true5 b ]) O0 u- c; Z: @
- mainButton.isLoaderVisible(); // true
2 V8 A: x" }6 P& H6 O - mainButton.isVisible(); // true
6 H9 a/ n3 ?% O" [$ a1 }: X% @$ r - mainButton.text(); // 'My text'
" F/ s0 D1 U1 h - mainButton.textColor(); // '#ffffff'5 F d! u. h O
7 F- n( d' u7 Z7 t- mainButton.state();2 j. _+ X- c! D- e
- // {# w5 V, E( f7 D- L; e k! p. a# p
- // backgroundColor: '#000000',$ R' M9 [2 @3 g3 l3 Z2 j
- // hasShineEffect: true,
) N; k% l6 ~: ~+ T - // isActive: true,
9 s$ i4 }# Y- k Y) P6 Q! g - // isLoaderVisible: true,% W# H: U3 m2 M
- // isVisible: true,
- ^0 m. s! O5 d/ M& p - // text: 'My text',; A3 s! }* l( l9 j" {" ~+ R- e
- // textColor: '#ffffff'" R A+ k1 H4 |, U& i- f
- // }
复制代码Functions : - import {6 q9 p4 Q- W, E3 [& \7 e
- setMainButtonParams,9 n ] R M3 K/ n0 }" h+ }
- mainButtonBackgroundColor,
. G% k, n. Y2 l - mainButtonHasShineEffect,0 h# g& ^& A* z3 Y6 g/ U( s
- isMainButtonVisible,
& _4 m: H6 J8 l! [2 S# u - isMainButtonEnabled,% |7 f, t) A" [6 z
- isMainButtonLoaderVisible,2 K! k: f2 w7 z4 l( n& n* q% t9 U8 V
- mainButtonState,# t% O. k& p0 E- x' y. M8 B0 t7 z
- mainButtonTextColor,- ` f8 y# o: q' {
- mainButtonText,3 y/ K- t6 k& G% G. w- V: U3 j
- } from '@telegram-apps/sdk';/ w/ }5 h* `0 {5 J7 X1 o2 h
- * K4 _' `! {, t8 P5 m
- setMainButtonParams({
0 u8 `) p7 `! ~3 A6 U8 m4 H! F0 } - backgroundColor: '#000000',
# v1 _2 z4 y5 E4 A% d1 s' R - hasShineEffect: true,
4 k, {7 n* }, G' z# A6 s2 S# [ - isEnabled: true,; l, W2 @# ` U% v
- isLoaderVisible: true,
( q6 E. o' N" l1 ]2 o) w6 `2 e - isVisible: true,
3 w6 d' o. P" Z6 v0 H- G) s - text: 'My text',) n/ l% X$ x, n* f& t
- textColor: '#ffffff'
, t( G7 C- P( V8 ~/ `% A$ P9 \ - });) ~5 V# d) t1 h" \
- mainButtonBackgroundColor(); // '#000000'. w4 U, f* V$ `) U3 |
- mainButtonHasShineEffect(); // true L$ k0 W6 L) |% z; `
- isMainButtonEnabled(); // true
/ f! _7 R, N+ [- |2 J k7 I6 K# X8 L' { - isMainButtonLoaderVisible(); // true
8 Y) [! t0 M4 h: ]5 Q - isMainButtonVisible(); // true
/ F @7 o* D8 e% e1 X! Q: F& c - mainButtonText(); // 'My text'1 p. n* Q) y0 k% f
- mainButtonTextColor(); // '#ffffff'
4 q# s. o$ `) y' N: \8 E, E - ! c- `) Q& N+ s7 M' U
- mainButtonState();; U, ^& @3 \( s& U! w
- // {
+ q4 \' t, M- ^( Y, Q7 F - // backgroundColor: '#000000',/ }7 \, z( I# i$ I
- // hasShineEffect: true,$ g. Z: x; q$ A) u; D$ G- B- ^. ?
- // isActive: true,
E5 H+ y, @0 Q& \$ Y: N - // isLoaderVisible: true,
! D; @# q, X# [- d7 M - // isVisible: true,9 n/ l& ?( z, \7 ]+ j% I" b
- // text: 'My text',
' K% e U, H: D6 X5 i - // textColor: '#ffffff'
! C3 @; K2 ^/ x, s - // }
复制代码 跟踪点击要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。 Variable : - function listener() {% l9 p( `7 m6 k7 \8 Y
- console.log('Clicked!');. g6 M/ v5 h `% ]5 {/ G8 D
- }5 b1 ^9 L8 f* j8 B" w
- - [; d" c, K4 K7 K7 m! T& z
- const offClick = mainButton.onClick(listener);; \! x% E% t6 g! M
- offClick();
9 V3 G, ]$ u) N4 p8 I; M9 M - // or) I7 Z3 @; {; c X( k7 ?
- mainButton.onClick(listener);
& c8 W- \3 h6 x& ~ - mainButton.offClick(listener);
复制代码Functions : - import {
' ^ B; H- E& I c6 r' y - onMainButtonClick,
3 S) g% k5 }1 f6 U8 [% V9 _ - offMainButtonClick,; \) o" s7 e+ t# d' j. c0 J4 }
- } from '@telegram-apps/sdk';
' }/ }. h% r) Y" d. X& ^- j - 3 k5 E s8 E W/ o0 W6 L
- function listener() {- V6 f% ?, n" x0 b5 Y4 k
- console.log('Clicked!');; c' Y! |/ N) {- h2 z
- }
: f! h% Q0 V7 {3 T w
5 L% Z1 ~, Q8 C! W, V- const offClick = onMainButtonClick(listener);7 j; N( D, P1 d- O9 j* f3 [
- offClick();
! t0 ^! U G- }6 w K - // or
) s% E e3 e1 }1 ^3 j - onMainButtonClick(listener);( v3 U3 s, X$ `( h" i, `
- offMainButtonClick(listener);
复制代码 $ s/ F& R8 E4 r! p5 \9 v+ Y
1 I+ c- p5 A! I
|