English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE คนไทย Français русский

1回答

0收藏

Telegram 小程序 | TypeScript @telegram-apps/sdk @2.x | 组件 | 辅助按钮

开源社区 开源社区 8483 人阅读 | 1 人回复 | 2025-02-25

辅助按钮

负责 Telegram 迷你应用程序二级按钮的💠组件

检查支持

要检查当前 Telegram 迷你应用程序版本是否支持辅助按钮,请使用 isSupported 方法:

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';
    / r( }0 l" W9 Q1 d7 a4 w7 |

  2. ' K  e. E. m' j0 O
  3. secondaryButton.isSupported(); // boolean
复制代码

Functions :

  1. import { isSecondaryButtonSupported } from '@telegram-apps/sdk';8 j4 u% i+ J. x. l+ n! t$ w

  2. ) @! r4 p: S, ~6 k
  3. isSecondaryButtonSupported(); // boolean
复制代码
安装

在使用该组件之前,有必要将其安装到配置正确的 属性中。 为此,请使用 mount 方法。 它将更新 isMounted 信号属性。

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';& N3 ?8 w# [7 e+ P5 {/ E; k5 I

  2. 9 y* Z- B/ M2 N8 d' q
  3. secondaryButton.mount();" w! Z) r) Y* U2 E& X
  4. secondaryButton.isMounted(); // true
复制代码

Functions :

  1. import {
    9 i5 Q+ Y" t, v" H3 V
  2.   mountSecondaryButton,( i9 E9 Y4 Y& Z) u
  3.   isSecondaryButtonMounted,
    3 V3 e; B& m- b9 Y! _* q  X/ U
  4. } from '@telegram-apps/sdk';
    + R, i! o/ j, E& L

  5. ) \2 B, w0 L. d* a+ X. R; S
  6. mountSecondaryButton();7 ]7 e" X$ X7 y& }  A. n2 i
  7. isSecondaryButtonMounted(); // true
复制代码
0 c. m4 j) o! l. r8 U

要卸载,请使用 unmount 方法:

Variable :

  1. secondaryButton.unmount();* F* z0 x9 |- L8 Q8 C# \1 F: T/ s) W
  2. secondaryButton.isMounted(); // false
复制代码

Functions :

  1. import {) K: V! d/ U2 P- o+ R
  2.   unmountSecondaryButton,* i" j. ~; j( [/ C& j! q  [1 p2 s
  3.   isSecondaryButtonMounted,) |, V8 q; e: t9 V( g# N7 e1 `. f
  4. } from '@telegram-apps/sdk';  ], s% d7 B: w$ q: K

  5. 2 Z" M' V  a9 ]5 Q* {8 W+ q* p
  6. unmountSecondaryButton();
    ( O. i$ ?/ v$ o, l) W
  7. isSecondaryButtonMounted(); // false
复制代码

WARNING

该组件的属性取决于 Mini AppTheme Params 组件的值。 具体来说,二级按钮使用 Mini App 的 bottomBarBgColor 和一些主题参数颜色。 在使用辅助按钮之前,请确保将这些组件安装到 。

设置属性

要更新按钮属性,请使用 setParams 方法。 它接受一个带有可选 属性的对象,每个属性负责各自的按钮特性。

反过来,调用该方法会更新 ,如 backgroundColor, hasShineEffect, isVisible, isEnabled, isLoaderVisible, position, state, textColor 和 text。

Variable :

  1. secondaryButton.setParams({
    + G6 ~9 i+ E6 f  v, c
  2.   backgroundColor: '#000000',  g# F5 d- u* p6 W6 H4 ?
  3.   hasShineEffect: true,0 N8 V" Z7 Q9 ^7 v: c
  4.   isEnabled: true,$ M5 D3 ~/ {0 k6 E* R# K" ?) X2 P
  5.   isLoaderVisible: true,1 c* H1 v: F( n1 ]' [
  6.   isVisible: true,; t' s$ B9 F- C/ _' g
  7.   position: 'top',# S; H' e& O$ R4 m2 U6 c
  8.   text: 'My text',
    5 B* S* }; `2 D( M3 U
  9.   textColor: '#ffffff'( I2 ]& i/ A  n0 X
  10. });) @3 H7 Y+ N/ ~8 {$ {7 L  y
  11. secondaryButton.backgroundColor(); // '#000000'
    $ O8 t* b+ m/ d$ t
  12. secondaryButton.hasShineEffect(); // true
    0 o  [' F" h! e
  13. secondaryButton.isEnabled(); // true* L* @1 v1 _7 c! T# z, N
  14. secondaryButton.isLoaderVisible(); // true
    5 P' Q" A+ f7 j) j+ B9 r+ {# p; ]
  15. secondaryButton.isVisible(); // true
    " C; Z! k  c7 e6 Y4 k' H
  16. secondaryButton.position(); // 'top'
    4 ?( \9 B# h4 Q% D( c0 D
  17. secondaryButton.text(); // 'My text'
    + y" H* U- v# _6 m1 x. R  O+ S' A9 s0 @# C
  18. secondaryButton.textColor(); // '#ffffff'
    4 C4 h& l0 K  u2 D# k% G
  19. * x/ l* v8 @+ Z8 M. t' c# T
  20. secondaryButton.state();
    5 C4 ]0 e! ]$ k6 ], `# j5 ^
  21. // {
    3 C6 ]& @6 i; z# M* T/ S4 [. ?
  22. //   backgroundColor: '#000000',5 v8 i) z5 H1 Q
  23. //   hasShineEffect: true,
    4 q) l/ l1 M2 F4 }5 Q5 y+ O
  24. //   isActive: true," x# t) t) [5 V- N6 _
  25. //   isLoaderVisible: true,
    3 o  U6 W, x* o' B/ J2 d' N
  26. //   isVisible: true,
    + n7 I+ ?3 c0 F  c: ~
  27. //   position: 'top',3 n9 Y. }# Q7 Y5 P" d4 |
  28. //   text: 'My text',% i' o6 F. M) M+ a$ R3 d+ t
  29. //   textColor: '#ffffff'
    * ^. d; ~+ g4 u. G& \0 M
  30. // }
复制代码

Functions :

  1. import {( l' S2 h6 X. E9 ^2 `: F" |
  2.   setSecondaryButtonParams,! c+ Q& L# A. k0 c/ Y3 p
  3.   secondaryButtonBackgroundColor,/ R6 ~3 y5 {$ ~% t! _- j
  4.   secondaryButtonHasShineEffect,
    / V8 c. [6 I1 P  h
  5.   isSecondaryButtonVisible,* B# b! W- h9 l
  6.   isSecondaryButtonEnabled,
    7 C5 Y, q( g5 N
  7.   isSecondaryButtonLoaderVisible,' J  \( {% @) T, j+ w% c; V6 @
  8.   secondaryButtonState,
    4 U2 C6 U! }% R+ t" a! C. X. i+ v
  9.   secondaryButtonTextColor,
    / P5 x& n5 T- _+ y9 t% G1 E
  10.   secondaryButtonText,# j/ s( L6 P, f* e
  11.   secondaryButtonPosition,: J  |- x3 Z. q1 Y$ {  Z; |0 P
  12. } from '@telegram-apps/sdk';$ w) a4 b' c% i! p8 L0 X0 S
  13. ' M7 q5 O9 T, V7 M
  14. setSecondaryButtonParams({
    / L2 |; g6 [+ {: o4 Y( e) y
  15.   backgroundColor: '#000000',) g* i7 _. y  t4 g8 u1 L
  16.   hasShineEffect: true,
    , u# n, ], L8 {9 L( o2 m! b+ q% l! ^
  17.   isEnabled: true,- A) n; P4 ?3 _
  18.   isLoaderVisible: true,3 h7 m6 @' K  f1 x8 {* ]& i# v
  19.   isVisible: true,
    4 f# n9 X: d7 l$ ?" e3 ~( O
  20.   position: 'top',  S4 Y, ~4 r& x4 h6 L9 z4 O
  21.   text: 'My text',5 k4 |/ l# G3 g: Z; q
  22.   textColor: '#ffffff'& t( r' I/ K4 c1 ~9 N
  23. });
    6 p8 M( v+ m3 }' I# u# x! j
  24. secondaryButtonBackgroundColor(); // '#000000'. I0 {9 r  D# X/ N0 Q
  25. secondaryButtonHasShineEffect(); // true' i* e) {1 v/ c$ c
  26. isSecondaryButtonEnabled(); // true
    5 y7 ?" ]" Y4 A: c. V- x7 @
  27. isSecondaryButtonLoaderVisible(); // true3 k) ~, n! E+ a7 _( L" @) z% x
  28. isSecondaryButtonVisible(); // true
    & H  Z3 y+ c2 `# B5 O/ b0 l! r
  29. secondaryButtonPosition(); // 'top'2 Q( _0 r  G; x
  30. secondaryButtonText(); // 'My text', D+ u9 d( u, b  z
  31. secondaryButtonTextColor(); // '#ffffff'
    ; K" r7 z. B9 }3 v$ i4 r
  32. 9 E- ]" V0 Z* [9 M4 {
  33. secondaryButtonState();9 ?6 B8 t/ Y0 ?/ C8 ^5 C% X6 K
  34. // {  v( Z; ?" D) w1 J  r4 L
  35. //   backgroundColor: '#000000',# K9 z+ I9 Q( Z9 N9 w( W  i
  36. //   hasShineEffect: true,
    1 P" _4 R: I/ ?2 s# i0 P
  37. //   isActive: true,: A; Y+ X: V: _' E8 o% t' P7 x: t
  38. //   isLoaderVisible: true,
    / B, F+ z! b4 i! F: [6 p% Z: `$ T6 b: r
  39. //   isVisible: true,
    + I  L- O7 [7 D/ r$ G
  40. //   position: 'top',
    ' L8 ^! \. s" f# Z
  41. //   text: 'My text',
    / q; V! j; }/ w6 j: W8 K  X, J9 H
  42. //   textColor: '#ffffff'" P) I% t/ G& {0 B1 V! I7 U& k
  43. // }
复制代码
跟踪点击

要添加按钮点击监听器,请使用 onClick 方法。 它返回一个函数,用于移除绑定的 监听器。 或者,您也可以使用 offClick 方法。

Variable :

  1. function listener() {" d+ X! C1 f6 M5 t4 i
  2.   console.log('Clicked!');
    ! C4 A* O% O+ L- }" W2 Y
  3. }8 Y, ]( f* O/ i- s. e' X% i( e
  4. " Q: Z# a0 S  l9 @0 o2 L5 r
  5. const offClick = secondaryButton.onClick(listener);3 K1 _& V# D# U* h5 c
  6. offClick();
    3 o5 W9 }7 H9 \1 ~- i5 u
  7. // or! v% Z6 B+ S3 M( x  q# `8 q  W/ P
  8. secondaryButton.onClick(listener);" U5 u; I: f3 n( w2 {- }
  9. secondaryButton.offClick(listener);
复制代码

Functions :

  1. import {
    % a, P  h; i3 k; V2 D2 R0 Z
  2.   onSecondaryButtonClick,
    ) r+ O# r- ]7 w! V* a3 u0 A
  3.   offSecondaryButtonClick,2 X% v0 d1 \7 x5 K
  4. } from '@telegram-apps/sdk';
    % \& N0 t( i" g
  5.   F7 `* @* W8 q/ z
  6. function listener() {
    4 W8 ?8 F! J1 x1 Q
  7.   console.log('Clicked!');
    2 Z3 L" h: R( y: B$ Y
  8. }" {( U) x5 d2 r+ O! @9 |7 E

  9. ' `% t5 _. Y( f2 W
  10. const offClick = onSecondaryButtonClick(listener);
    4 q& o7 z! W/ Z7 D! F1 [( v+ Y
  11. offClick();% D4 Z: f6 ?# |
  12. // or
    - C- l" q8 ~* v! w' I2 T* ]/ r
  13. onSecondaryButtonClick(listener);
    " x4 w3 o4 o* [! }: S
  14. offSecondaryButtonClick(listener);
复制代码
) V. ~' U! N+ s) N# q
; `8 d& o# Y0 b' m2 a
分享到:

回答|共 1 个

农1885

发表于 2025-2-26 18:28:00 | 显示全部楼层

回开源帖是美德!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则