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

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

1回答

0收藏

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

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

辅助按钮

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

检查支持

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';
    ) u6 t" e& ~" i, B8 z( S) T" L( s

  2. 5 ~3 f; E5 M% }5 k
  3. secondaryButton.isSupported(); // boolean
复制代码

Functions :

  1. import { isSecondaryButtonSupported } from '@telegram-apps/sdk';
    4 y; W- D: L/ I/ ?  I8 x1 ^
  2. - _- v# P+ f1 G' |  X' v" X
  3. isSecondaryButtonSupported(); // boolean
复制代码
安装

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';
    9 K! }* r' e  P; X: `6 V

  2. 1 o$ l: U" g+ M* O" m% f7 S7 q
  3. secondaryButton.mount();
    * Y* b; W3 Z; l# i6 M' e  N4 l4 M
  4. secondaryButton.isMounted(); // true
复制代码

Functions :

  1. import {
    ' G+ F4 @2 F& E3 t$ F
  2.   mountSecondaryButton,
    1 a& x2 I! q; ?  i$ B+ H# Z) Z
  3.   isSecondaryButtonMounted,
    $ R' `7 k% d7 W
  4. } from '@telegram-apps/sdk';
    . O& n+ h! V. o9 j$ {; a

  5.   u. z0 m' m6 [  h9 `* d2 W
  6. mountSecondaryButton();  }* T1 x6 h1 |; c: f3 T
  7. isSecondaryButtonMounted(); // true
复制代码
* q1 ]- S4 a, A/ k& c9 ]$ X9 E

要卸载,请使用 unmount 方法:

Variable :

  1. secondaryButton.unmount();; X* e0 b! ]1 \3 j/ a3 y
  2. secondaryButton.isMounted(); // false
复制代码

Functions :

  1. import {$ Q2 X# Y) G) A9 t5 A" J( T
  2.   unmountSecondaryButton,& L  [/ h3 ]7 R) y  V0 p  p6 T
  3.   isSecondaryButtonMounted,
    . Y* |, C8 ~3 F1 {4 O
  4. } from '@telegram-apps/sdk';$ n# Z+ w' `1 c+ W

  5. 1 f: `6 p% C" b7 q1 v! a
  6. unmountSecondaryButton();
    ; b1 @4 r/ M2 M; n# W$ J9 G7 `) {3 n) B
  7. isSecondaryButtonMounted(); // false
复制代码

WARNING

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

设置属性

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

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

Variable :

  1. secondaryButton.setParams({
    * N$ _5 D* k# k& i; o
  2.   backgroundColor: '#000000',
    6 L. B+ c$ j, t! q
  3.   hasShineEffect: true,* H) P! R, v3 @
  4.   isEnabled: true,; A# B* N" y) r& Q3 M+ f
  5.   isLoaderVisible: true,
    $ \% k# x7 r+ F% g4 z( W: E! m* }
  6.   isVisible: true,
    9 g2 A& w- P% h* ^+ r
  7.   position: 'top',
    , G+ s/ j; N# I5 y- Y
  8.   text: 'My text',
    ( R  z7 P7 a% c, c4 ~
  9.   textColor: '#ffffff'! J0 W- A" C! _# x7 @9 T
  10. });+ N2 o& o3 @0 z) K9 h- i2 R& V
  11. secondaryButton.backgroundColor(); // '#000000'
      W4 W$ ~! `8 ?4 C8 J& M
  12. secondaryButton.hasShineEffect(); // true
    - ^& G: N3 _2 ]
  13. secondaryButton.isEnabled(); // true  c7 ]7 h- U; R/ S7 h2 h
  14. secondaryButton.isLoaderVisible(); // true9 ]0 d7 N+ q8 I. O  B* k, h
  15. secondaryButton.isVisible(); // true
    6 M2 O* H: J' V( G' B+ Z
  16. secondaryButton.position(); // 'top'
    6 Z# P5 @/ y' y; Y) J  N, b2 X
  17. secondaryButton.text(); // 'My text'
    3 X+ p. j! d" N+ Y7 c, j7 t
  18. secondaryButton.textColor(); // '#ffffff'
    & o- Z- Q9 |" ?
  19. ' `7 u6 s; i& n6 N2 ]1 v" \: ~
  20. secondaryButton.state();% Q, _7 t5 B! Y* k9 _
  21. // {5 D0 z7 M" p# `9 D
  22. //   backgroundColor: '#000000',, `8 Q1 ~% R0 ]: D/ z4 A
  23. //   hasShineEffect: true,
    ; x+ a# q  E7 N- E
  24. //   isActive: true,; z& u1 }) C7 {; a7 L: X2 k9 R
  25. //   isLoaderVisible: true,1 N$ {0 T. `4 ^5 L& K( I5 g# T
  26. //   isVisible: true,
    9 d7 j9 m$ ?+ v
  27. //   position: 'top',
    , L) N' o7 e9 {1 _
  28. //   text: 'My text',/ j$ r) I9 ~: x9 ]8 T
  29. //   textColor: '#ffffff'  I' C( ?- a) k0 q4 k4 L
  30. // }
复制代码

Functions :

  1. import {
    ( m2 i, l# J1 ^: Y4 J$ R
  2.   setSecondaryButtonParams,
    ! W  v8 v$ E. P* {& @% F
  3.   secondaryButtonBackgroundColor,5 X. @7 z7 A5 D, t
  4.   secondaryButtonHasShineEffect,+ d) W" P& H( p1 e. s
  5.   isSecondaryButtonVisible,
    0 i/ n8 p# h& Y/ n
  6.   isSecondaryButtonEnabled,1 C1 n- C8 ^& y" E) f$ t
  7.   isSecondaryButtonLoaderVisible,
    5 P. C  t3 X! C4 J
  8.   secondaryButtonState,
    ; N7 h" _! ?/ U
  9.   secondaryButtonTextColor,: W5 f$ m1 K; V4 Y
  10.   secondaryButtonText,/ ]" ^9 U  K7 j. C9 J, H% ~
  11.   secondaryButtonPosition,; J3 J& B. v, i3 h. b! v
  12. } from '@telegram-apps/sdk';
    4 {9 T! B3 ^; ]4 \( g
  13. 3 L5 K4 f9 l  d- w: `
  14. setSecondaryButtonParams({
    7 d6 O4 P% w4 i8 z0 g2 r4 g( ~- i
  15.   backgroundColor: '#000000',* b# l' `* A0 `, B3 d1 p
  16.   hasShineEffect: true,
    + F- G  {% E" _5 c- `  ]0 G
  17.   isEnabled: true,
    5 c6 q7 h( j% X* W9 E8 R
  18.   isLoaderVisible: true,1 X) O* @4 V8 N4 _
  19.   isVisible: true,5 p* [# |2 P, q9 b; z
  20.   position: 'top',
    " n6 y+ Y  L+ S  m5 s. a' Q, G
  21.   text: 'My text',$ ?1 c7 c6 O, k
  22.   textColor: '#ffffff'+ C0 T6 }$ ^0 \8 @6 ~" Y7 d. e3 ]% T
  23. });& n; d; q( Y4 {% p; i. D) \' G6 j, X
  24. secondaryButtonBackgroundColor(); // '#000000'
    : O( n5 u; W; J6 n
  25. secondaryButtonHasShineEffect(); // true- ~. L- F: b" X( n" C! e8 g, X( l
  26. isSecondaryButtonEnabled(); // true& B% }- ?2 s% z- M8 G% k' i
  27. isSecondaryButtonLoaderVisible(); // true0 T" A/ X5 M3 m3 e9 Y
  28. isSecondaryButtonVisible(); // true7 @7 ~* z4 c/ E; l" v8 v
  29. secondaryButtonPosition(); // 'top'( }; }# C9 I8 S& R" ~' T1 j
  30. secondaryButtonText(); // 'My text'
    $ H' ], t  t* M' A
  31. secondaryButtonTextColor(); // '#ffffff'2 y& \4 N7 F0 F2 L

  32. / f: N# O. a7 q0 s: A- X
  33. secondaryButtonState();8 O4 v+ w1 I9 M; g% v9 }
  34. // {! k! |; w9 j' x- W; a. I
  35. //   backgroundColor: '#000000',
    5 N( M" S& g, q9 g1 e
  36. //   hasShineEffect: true,
    ! @" c2 E7 s# W6 F" S
  37. //   isActive: true,
    3 S3 d9 M' a7 |- l5 G  u! S  U
  38. //   isLoaderVisible: true,1 U6 g7 H$ V6 A5 D/ _
  39. //   isVisible: true,
    8 m) @, w$ X6 R$ S) {0 v$ z
  40. //   position: 'top',
    0 G2 Y8 ^0 C% D* P8 E4 v' {
  41. //   text: 'My text',
    ( u7 r% \4 a" k
  42. //   textColor: '#ffffff'
    ; U/ t# v- h. `+ _2 r; F( k
  43. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {" [/ U5 e' M- N+ D) x' w* l
  2.   console.log('Clicked!');3 m- s6 C# y& L
  3. }
    , A  e0 E* g5 t  d0 K

  4. + D! N  E9 c0 Z, H4 p- e/ C
  5. const offClick = secondaryButton.onClick(listener);
    $ u. Q/ Y& X1 i  ~/ }- S8 G3 g" ?. n
  6. offClick();. X& w8 R, ^0 r+ R! F
  7. // or/ r5 C9 I$ O' v9 _) C5 p& z
  8. secondaryButton.onClick(listener);
      U( u8 ?+ F. ~" G- \% T' ]
  9. secondaryButton.offClick(listener);
复制代码

Functions :

  1. import {
    ' c: r+ [& e" r" |- e
  2.   onSecondaryButtonClick,# R; ^( E2 ]5 y7 S5 y% W  T! v
  3.   offSecondaryButtonClick,
    0 f0 I. _% ^( L3 B; p
  4. } from '@telegram-apps/sdk';" Y2 j+ E/ A8 y2 G/ I& Q
  5. 4 j6 \5 ^0 ^% h3 k3 y, s$ q
  6. function listener() {
    " ?" J; J  {, V) [1 \
  7.   console.log('Clicked!');
    ) O, L3 `( @; @, j0 Q
  8. }
    , X4 F& W( ?: ]3 Q

  9. ; K: l) ]& z0 A9 E
  10. const offClick = onSecondaryButtonClick(listener);8 [3 x& Q* G0 ~% J! u
  11. offClick();. f4 v4 p; H! m. k* a( n# u& s
  12. // or. c9 F/ d! W% o& N9 e
  13. onSecondaryButtonClick(listener);
    + O2 F1 {* _( W
  14. offSecondaryButtonClick(listener);
复制代码
+ i3 w* {8 c: i" B4 x0 P
& v3 \5 E- `: H( P. [
分享到:

回答|共 1 个

农1885

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

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

本版积分规则