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

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

1回答

0收藏

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

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

辅助按钮

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

检查支持

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';$ q  y4 e, W' b. d; _1 G+ S

  2. - l3 M' _2 N7 R
  3. secondaryButton.isSupported(); // boolean
复制代码

Functions :

  1. import { isSecondaryButtonSupported } from '@telegram-apps/sdk';% p0 R& |7 D! |

  2. : ?& Q. r1 Z0 `1 z( v( I
  3. isSecondaryButtonSupported(); // boolean
复制代码
安装

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';
    . N2 X0 a2 c0 x$ L3 P, r

  2. 5 k2 A& Q6 U  p' T
  3. secondaryButton.mount();
    1 R4 k$ s, s& ^- C% ~. n
  4. secondaryButton.isMounted(); // true
复制代码

Functions :

  1. import {: M. b+ @) s, M" a8 w
  2.   mountSecondaryButton,
    ( k% ?% f4 C9 \
  3.   isSecondaryButtonMounted,/ y3 t0 w: k& Y6 D9 R0 z. b
  4. } from '@telegram-apps/sdk';5 H2 z. o; y7 p/ I( w3 g' z

  5. + ^, j, F7 Y2 b
  6. mountSecondaryButton();
      e2 E( m& U2 C9 R; V
  7. isSecondaryButtonMounted(); // true
复制代码

7 V4 W8 b7 U9 N) g2 l

要卸载,请使用 unmount 方法:

Variable :

  1. secondaryButton.unmount();* P, n1 E2 @3 e2 m
  2. secondaryButton.isMounted(); // false
复制代码

Functions :

  1. import {
    + h  _* n9 q- ?  s' G* d
  2.   unmountSecondaryButton,
    & W4 m. {/ V- W$ F$ a7 m
  3.   isSecondaryButtonMounted,
    8 ^$ c) j. ]8 }8 @! F
  4. } from '@telegram-apps/sdk';
    3 a& w( {" D- W' ^( z, e+ e' b6 A0 h

  5. 6 I7 S0 B& g: Y9 u3 J" F0 j3 @' h
  6. unmountSecondaryButton();0 E8 N% f! P3 Q+ R3 P) y$ T. C/ F
  7. isSecondaryButtonMounted(); // false
复制代码

WARNING

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

设置属性

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

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

Variable :

  1. secondaryButton.setParams({+ P' Z' f6 P% R. K
  2.   backgroundColor: '#000000',! K( ]: N" X- h) G% D
  3.   hasShineEffect: true,
    " A2 k$ N& i% p8 y+ {1 N' Y
  4.   isEnabled: true,% E- B# N2 K6 w0 x0 O
  5.   isLoaderVisible: true,
    9 B; t  f2 g) }, f
  6.   isVisible: true,
    # V: N$ B* I0 V$ u
  7.   position: 'top',! W" L6 p9 L; j2 W/ p3 P" |, d  I
  8.   text: 'My text',: a" ]9 g- J4 q8 A/ Z0 e
  9.   textColor: '#ffffff'
    . V6 x9 `2 o& [- T
  10. });
    3 {4 h" O4 g( I* l' X& F
  11. secondaryButton.backgroundColor(); // '#000000', ]) k  C- h) s) Z5 t
  12. secondaryButton.hasShineEffect(); // true  c- `! h7 F# E  U0 P1 O
  13. secondaryButton.isEnabled(); // true
    7 t) L- g6 S4 e# h! E
  14. secondaryButton.isLoaderVisible(); // true
    $ U8 h+ [% \2 s+ a% c
  15. secondaryButton.isVisible(); // true9 A/ v& \; F7 o9 Z' W
  16. secondaryButton.position(); // 'top'8 Q7 S) H6 C4 h) x1 q
  17. secondaryButton.text(); // 'My text'
    0 e# r; M& ], u' P5 c" Y% {2 n1 J
  18. secondaryButton.textColor(); // '#ffffff'
    ' @$ _6 u# R& G% z5 D4 ~7 m6 d

  19. : d  u1 m( f. Q
  20. secondaryButton.state();6 [+ r8 o' ^" Z' W
  21. // {
    # v7 ^- m; j; X% H4 Z6 q; t9 p% t' h4 [* m* j
  22. //   backgroundColor: '#000000',
    8 v; D0 Y4 x/ X1 e9 L+ E
  23. //   hasShineEffect: true,
    ) p* R2 B/ }. @/ u3 \+ z
  24. //   isActive: true,
    8 y# Q" c+ P7 G
  25. //   isLoaderVisible: true,7 C. p# \* F2 k( h( n
  26. //   isVisible: true,
    - `1 J8 N, H' B! v9 E; F' M
  27. //   position: 'top',; ]4 R$ Y' _) y: o/ D
  28. //   text: 'My text',& {) s* L; S2 ?) u
  29. //   textColor: '#ffffff'
    ) K# r2 N- @; q0 y2 y
  30. // }
复制代码

Functions :

  1. import {
      i! A3 X: N. m, [4 `
  2.   setSecondaryButtonParams,) Z, W( h- |1 `4 I% q5 }
  3.   secondaryButtonBackgroundColor,
    ! R5 ^5 B, M+ F% V
  4.   secondaryButtonHasShineEffect,
    ( f  a2 ?0 s+ W- j* i1 [  k; X
  5.   isSecondaryButtonVisible," d1 S: q( F7 l  R4 w, ?
  6.   isSecondaryButtonEnabled,$ M& V( g: i+ l, s" r
  7.   isSecondaryButtonLoaderVisible,$ P* a( G9 I! l+ K3 Z* T
  8.   secondaryButtonState,8 u7 K7 ?1 Z* w7 S% q
  9.   secondaryButtonTextColor,
    8 |) |. z: f  N. s' ^1 n
  10.   secondaryButtonText,
    9 l  A# W2 G5 ]1 m) ]  j3 F
  11.   secondaryButtonPosition,8 N  l1 Q) N: m# `5 a/ N
  12. } from '@telegram-apps/sdk';
    ( _9 J' M  U  h2 }
  13. 8 l7 q+ X+ W9 k& \! W9 f5 H& J
  14. setSecondaryButtonParams({
    : X, @. o" H4 u, o3 d) Y9 I
  15.   backgroundColor: '#000000',
    % K$ O4 ~: W1 b/ M0 T7 j  e
  16.   hasShineEffect: true,  a0 m- b- {  Y; x1 F0 k% `
  17.   isEnabled: true,
    ; d" j" g( G3 c1 @6 p
  18.   isLoaderVisible: true,( l( B& g- E* U. i2 E* r4 m/ o8 N
  19.   isVisible: true,, h2 P2 y' u4 y( y
  20.   position: 'top',
    " U. @) x8 @5 b# M: L. m9 Z+ e) D
  21.   text: 'My text',8 e& h, c2 `$ Y/ ~/ C6 r
  22.   textColor: '#ffffff'
    / [! g$ X( K  h  P; R+ H+ r
  23. });3 K6 v/ |" k1 w& V
  24. secondaryButtonBackgroundColor(); // '#000000'# F- m/ v) g/ g9 o: q) t
  25. secondaryButtonHasShineEffect(); // true
    / Q9 o* G, a+ G" w+ k  l
  26. isSecondaryButtonEnabled(); // true8 ~6 G+ s: l$ U. {
  27. isSecondaryButtonLoaderVisible(); // true8 f& I6 @8 O6 c
  28. isSecondaryButtonVisible(); // true
    ! Z* ]8 [& U8 ?2 P. \# l
  29. secondaryButtonPosition(); // 'top'
    2 V% W0 g) Q1 x) L7 O' t
  30. secondaryButtonText(); // 'My text'+ @2 h2 [  z! m, c4 Q5 b$ Y
  31. secondaryButtonTextColor(); // '#ffffff'/ N1 T3 h. T- f; o
  32. - o; G& j5 t0 O+ c1 r
  33. secondaryButtonState();
    6 J# @1 U$ e$ v( H" \3 `: [- L& s% x
  34. // {- x- k7 N0 V1 `5 I( O  H3 A+ w
  35. //   backgroundColor: '#000000',
    / r) x0 d2 H3 c9 F& p; a
  36. //   hasShineEffect: true,
    . T+ l8 y( M, q
  37. //   isActive: true,
    7 h1 O" W, p9 d9 v- _( j
  38. //   isLoaderVisible: true,
    & d, J6 m. q6 W- d7 h4 I
  39. //   isVisible: true,
    9 j# M8 m% Z$ H$ E8 X* \: @
  40. //   position: 'top',
    1 k* M( r8 k& A6 |2 X) V
  41. //   text: 'My text',: |. o. l, p8 M( W
  42. //   textColor: '#ffffff'- j" W- c# [6 L. C$ H5 Q
  43. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {6 c$ `6 F) Z. r* p" ^; Q- n
  2.   console.log('Clicked!');
    ' k& u  J7 k3 p+ b
  3. }
    . r% }2 d9 l. d0 a- d

  4.   _8 _/ b' `$ t4 @1 s$ E1 k1 c
  5. const offClick = secondaryButton.onClick(listener);8 e4 z( m& p' l- B4 U2 p. u3 [
  6. offClick();
    ! g7 ^1 O' D% }" R) l5 v# K0 W9 t# I
  7. // or* w0 B, u7 V$ I8 b% a: P$ [; x
  8. secondaryButton.onClick(listener);4 r  P( g3 |6 S' B2 I9 y
  9. secondaryButton.offClick(listener);
复制代码

Functions :

  1. import {, _$ `$ n' C5 M: N+ k6 w
  2.   onSecondaryButtonClick,. U/ l6 o2 M1 h/ B
  3.   offSecondaryButtonClick,
    8 S2 v' o- e: H* S  U+ P) b
  4. } from '@telegram-apps/sdk';
    5 X% n0 I. l3 L! Y: h
  5. 2 |% B; z# }, i4 S* \
  6. function listener() {
    4 ^& u3 B; ?- u1 H; J0 F% j5 R3 E! [
  7.   console.log('Clicked!');
    , {( X& k. H0 e& [4 A$ w* P# F
  8. }
    9 [# u) F, p" y+ M% \5 u
  9. * Q# l1 l  r# Q4 O# g1 J, y) o
  10. const offClick = onSecondaryButtonClick(listener);9 ~' w5 `$ O: V4 e
  11. offClick();
    # h( j3 y/ v( l2 }4 @& b5 I+ o) o  w
  12. // or
    ( S$ M7 a9 S1 V  A
  13. onSecondaryButtonClick(listener);( y5 i1 ~9 ~+ \3 ?: y, @0 o3 ?
  14. offSecondaryButtonClick(listener);
复制代码
3 w  X# [; h2 K0 F; c, \3 C9 a  i
$ O% h9 Q$ \1 n9 u+ A7 r% z' [' G5 t* |
分享到:

回答|共 1 个

农1885

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

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

本版积分规则