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

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

1回答

0收藏

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

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

辅助按钮

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

检查支持

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';( ?! y4 K/ D: B! E# W

  2. 4 T3 Y& p' `* z% F9 \& g! H
  3. secondaryButton.isSupported(); // boolean
复制代码

Functions :

  1. import { isSecondaryButtonSupported } from '@telegram-apps/sdk';1 G  b/ c: S, u
  2. " C; C: c" m( B& {% k7 F' v
  3. isSecondaryButtonSupported(); // boolean
复制代码
安装

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

Variable :

  1. import { secondaryButton } from '@telegram-apps/sdk';
    # B0 [7 E; H5 z( d+ b% ~) v
  2. % ^# S* r! z5 E( T& x; ]8 S
  3. secondaryButton.mount();
    9 z# E5 V- ^1 `" v6 H
  4. secondaryButton.isMounted(); // true
复制代码

Functions :

  1. import {5 M( b* p& i; J" N
  2.   mountSecondaryButton,
    ' C. v3 a" K- W  L( o; ^- v$ _7 U
  3.   isSecondaryButtonMounted,
    5 `2 @: o3 D2 j, x- o
  4. } from '@telegram-apps/sdk';! T- c$ ?! H8 {: F: S* c
  5.   U3 G# T- [! |& w5 _' @3 {
  6. mountSecondaryButton();# j% ?9 n4 b1 p& C+ Y; R0 W. m8 W
  7. isSecondaryButtonMounted(); // true
复制代码

, O2 v/ u* z, O2 J

要卸载,请使用 unmount 方法:

Variable :

  1. secondaryButton.unmount();
    " |3 B9 {* u# J# S& P' ~
  2. secondaryButton.isMounted(); // false
复制代码

Functions :

  1. import {0 g$ Q7 U5 E3 d9 d* g: O9 g; x- S
  2.   unmountSecondaryButton,' U3 ]- U6 o' u+ Z' |
  3.   isSecondaryButtonMounted,. T9 B3 S2 y) j9 s3 _* b
  4. } from '@telegram-apps/sdk';
    0 B- O  m6 }$ g# m
  5. ! J' n( M4 z& l" s; F; F" s$ j: N
  6. unmountSecondaryButton();
      l& ^$ w4 U, P' y; Q* [- 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({* a* D% o5 ]4 h" R$ F- U
  2.   backgroundColor: '#000000',5 @  D- U7 c9 p/ T# }* I5 U# m
  3.   hasShineEffect: true,
    - y, e, I. v$ Y: j3 \- m
  4.   isEnabled: true,. w- F" O1 e/ B
  5.   isLoaderVisible: true,
    & x* }/ |" z7 W! P3 ^* X' \" ]- a
  6.   isVisible: true,
    & Y% i+ z/ f0 y7 p
  7.   position: 'top',: n9 K6 |+ D7 n+ A6 o! ~
  8.   text: 'My text',6 }' ~9 U! t0 A$ q
  9.   textColor: '#ffffff'4 t5 ^& v/ ^; ~+ D) E9 M, z
  10. });
    + L4 x3 |9 s' ^+ W8 T8 V( \
  11. secondaryButton.backgroundColor(); // '#000000'5 {2 W4 ^. I- z1 H5 l
  12. secondaryButton.hasShineEffect(); // true
    5 K* U/ P1 \! T
  13. secondaryButton.isEnabled(); // true1 @9 D) b- f) y  w, {2 F
  14. secondaryButton.isLoaderVisible(); // true" r4 \' F9 \. }9 P8 m; [* c. z- l0 d
  15. secondaryButton.isVisible(); // true
    ! S: @7 ~; a( c+ V
  16. secondaryButton.position(); // 'top'
    - o- K2 \  C  v, q( j8 h- k
  17. secondaryButton.text(); // 'My text'$ k. G4 P3 O/ t6 i# g
  18. secondaryButton.textColor(); // '#ffffff'
    6 B1 O, `6 x4 N1 V, h* ]

  19. ( e+ ?5 |+ _5 d# g6 j  z1 Z* Y- F; g) B7 c
  20. secondaryButton.state();$ _/ n2 K2 N9 y. U. h* r0 h
  21. // {3 A, ~' H1 V% R8 f
  22. //   backgroundColor: '#000000',7 M' k7 r6 w. R* I; e
  23. //   hasShineEffect: true,4 K& o) o. O. c+ i* L' ~
  24. //   isActive: true,& n3 w' {$ H. C7 {
  25. //   isLoaderVisible: true,) y, x) a4 M3 R
  26. //   isVisible: true,7 `4 p5 Z3 Q4 F" l
  27. //   position: 'top',  p8 _) P2 N, d5 k6 a- z8 M0 w
  28. //   text: 'My text',
    5 X) E0 ^. A/ D/ r
  29. //   textColor: '#ffffff'
    + n3 P0 |/ a5 C
  30. // }
复制代码

Functions :

  1. import {
    % K7 Y. L. S9 d2 i5 f5 ?
  2.   setSecondaryButtonParams,
    # [0 x, `& t. _% a5 I: B
  3.   secondaryButtonBackgroundColor,
    2 ]% X- k" h& l2 M
  4.   secondaryButtonHasShineEffect,
    9 M# `5 m  J6 b' C3 E
  5.   isSecondaryButtonVisible,4 {. y8 @5 e3 c/ V: f
  6.   isSecondaryButtonEnabled,; O5 t, h& A4 q7 n2 B3 T6 A
  7.   isSecondaryButtonLoaderVisible,
    3 a  b5 _# F4 D; s3 H' h  R3 x
  8.   secondaryButtonState,
    & [! w' W( U3 w. C5 |( `, O
  9.   secondaryButtonTextColor,
    / m, Z9 L0 D1 B0 V& [: l
  10.   secondaryButtonText,
    0 n! z- p* V0 N3 {" @
  11.   secondaryButtonPosition,6 Q% n$ m4 N" H' s2 ?
  12. } from '@telegram-apps/sdk';
    $ B& w0 K2 [7 i. _; ^2 M* Y  L
  13. 9 l0 V2 Q; G+ A5 v* T( M. l9 y
  14. setSecondaryButtonParams({4 q8 O9 C9 E$ G3 x2 M
  15.   backgroundColor: '#000000',! z, G  q# h3 G
  16.   hasShineEffect: true,
    & @- f) s+ L. y2 K  c; |
  17.   isEnabled: true,
    9 X' N- m8 V3 j" v( ]+ A2 A3 ~7 _
  18.   isLoaderVisible: true,. B  A* v7 f# G& V
  19.   isVisible: true,, Z" V( M% O4 ]+ C
  20.   position: 'top',
    0 N  r: ^5 e2 d/ c
  21.   text: 'My text',( Z2 f* Q8 [& Z# @
  22.   textColor: '#ffffff'
    # B' ?8 N7 y. H1 X2 G
  23. });: j; b& t( Z1 }2 H. R
  24. secondaryButtonBackgroundColor(); // '#000000'( H/ {1 ^6 x$ U5 A
  25. secondaryButtonHasShineEffect(); // true
    8 B! e4 c; h% T9 L) R3 o
  26. isSecondaryButtonEnabled(); // true9 A, E2 }3 @* r8 u3 H
  27. isSecondaryButtonLoaderVisible(); // true
    6 Y1 w. G" X( |4 ^! Z# Q' G# Z
  28. isSecondaryButtonVisible(); // true6 b* K; R5 C# S+ R  b8 k" M
  29. secondaryButtonPosition(); // 'top'
    4 Z  @% `$ ^) u; [9 S" {
  30. secondaryButtonText(); // 'My text'5 T5 e2 U6 r- \
  31. secondaryButtonTextColor(); // '#ffffff'
    ( j, B/ B+ B6 s8 `. c7 n" E2 @

  32. & {- x* F: y5 A% r$ @
  33. secondaryButtonState();/ A) M& L  _  U3 o( q5 L+ I
  34. // {
    9 _: A3 F: R9 f- O2 l; {$ v, X% T
  35. //   backgroundColor: '#000000',
    , f7 l$ y( o+ d8 g" X. @
  36. //   hasShineEffect: true,
    ' T, T: G6 _' c8 c& G& V  a  I
  37. //   isActive: true,
    ! h* Q2 K! q! b# e: \- g1 D
  38. //   isLoaderVisible: true,
    ; T, h: n( W# L
  39. //   isVisible: true,( `4 S/ L/ f: x9 w
  40. //   position: 'top',2 B) ~  z& K" S6 a2 m/ P  X+ v' h& G+ ?
  41. //   text: 'My text',% a# e9 p5 E! X' D8 R3 K
  42. //   textColor: '#ffffff'
    6 L* P+ T! @/ c7 H
  43. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {
    9 S8 m- \" m! y% `3 e
  2.   console.log('Clicked!');" z0 I8 Z& Y0 b+ G# x' D  f" n
  3. }2 `" `0 g! C4 O/ z
  4. 0 {5 S9 w6 a5 X; h
  5. const offClick = secondaryButton.onClick(listener);8 Z+ H0 f9 V1 I2 H* F
  6. offClick();% Y" l/ S- t* _1 \
  7. // or# S! `  `) |8 r
  8. secondaryButton.onClick(listener);0 W" s# n0 _- O3 h( v
  9. secondaryButton.offClick(listener);
复制代码

Functions :

  1. import {
    + b! {, N3 V% p1 c+ n
  2.   onSecondaryButtonClick,
    5 O6 I) `- y: Z6 l* {/ U0 Z
  3.   offSecondaryButtonClick,
    ( b5 j! {) i5 u- C" P* k  f; V
  4. } from '@telegram-apps/sdk';
    6 l9 l9 q( ~8 N( l

  5. $ }. f- ?( [7 ?% [( r& X* q
  6. function listener() {
    5 L( Z% B1 q8 H1 j% }
  7.   console.log('Clicked!');$ k8 ?' ~: d- V( {7 U
  8. }
    ( P0 j' I6 t- r) E

  9. ; j( d- o" C1 o5 T# j' p
  10. const offClick = onSecondaryButtonClick(listener);2 r, F: ]. y9 T
  11. offClick();/ K  k8 e; P( g' O  k
  12. // or$ g0 h, }& ~% l9 E& ~: Q
  13. onSecondaryButtonClick(listener);
    * C( M% U8 o+ [7 b9 H
  14. offSecondaryButtonClick(listener);
复制代码

3 W8 U- C" j2 o) N, I0 Q. M' x0 I4 J) K/ o
分享到:

回答|共 1 个

农1885

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

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

本版积分规则