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

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

回答

收藏

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

开源社区 开源社区 7648 人阅读 | 0 人回复 | 2025-02-25

主按钮

负责 Telegram Mini Apps main button 的💠组件

安装

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

Variable :

  1. import { mainButton } from '@telegram-apps/sdk';, r8 u2 y) y5 M( d; x

  2. - k* S5 t8 J' J! r/ `' N, f
  3. mainButton.mount();
    * Z% o: L. W% y4 V; V$ e- G5 H
  4. mainButton.isMounted(); // true
复制代码

Functions :

  1. import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
    4 D9 {' ~2 s& h! l: L6 L# O

  2. , q' J+ O5 T1 L4 n5 T1 n
  3. mountMainButton();
    5 g* y2 q% {6 s; B: |" o
  4. isMainButtonMounted(); // true
复制代码
. ]9 U  i' ]) ^; v! l

要卸载,请使用 unmount 方法:

Variable :

  1. mainButton.unmount();
    4 `/ [) X9 G4 \* x0 g
  2. mainButton.isMounted(); // false
复制代码

Functions :

  1. import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
    : Y' i" E4 r3 q
  2. ) }; j7 c  Y6 Y2 @
  3. unmountMainButton();3 x( Z2 \2 ]3 o% o4 D5 O6 h
  4. isMainButtonMounted(); // false
复制代码

WARNING

该组件的属性取决于 主题参数 组件中的值。 使用主按钮前,请务必安装主题参数。

设置属性

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

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

Variable :

  1. mainButton.setParams({) D! `. L! S. @% X, [4 A$ e
  2.   backgroundColor: '#000000',9 e: t2 t& r1 {
  3.   hasShineEffect: true,
    ! A# s9 F  N7 l4 e0 G: j  H! A
  4.   isEnabled: true,- c1 v' b$ }% g- Y: U6 Q. e
  5.   isLoaderVisible: true,
    & ^- u3 s2 j- p- r
  6.   isVisible: true,
    / T  ~; T# E' e7 o6 W1 b
  7.   text: 'My text',
    + v: \1 t# N" B9 V( O" H
  8.   textColor: '#ffffff'- W0 g3 c' o0 u1 ?6 G
  9. });, D$ g% l! N+ Y" {. ?8 D5 B
  10. mainButton.backgroundColor(); // '#000000'
    + C, ]7 \& b% ~
  11. mainButton.hasShineEffect(); // true
    ) e: X0 S6 }' k# e9 \# L. J( [
  12. mainButton.isEnabled(); // true
    " z8 V$ k, j% D! n' c% n) q1 |  ?
  13. mainButton.isLoaderVisible(); // true
    1 x) a; T. k0 D7 p+ F6 V7 j, S6 I
  14. mainButton.isVisible(); // true% q) @2 u1 v' m' H9 ]% |' z! ~5 z4 A
  15. mainButton.text(); // 'My text'
    ' [6 X4 o3 Z1 I3 `3 ?
  16. mainButton.textColor(); // '#ffffff': i4 X; P9 |% h3 ^6 l
  17. 4 @3 a' I2 l& Y. ]
  18. mainButton.state();9 l+ t( q  x# }6 W4 I) ?
  19. // {5 `1 i$ b# w/ L6 U) Z
  20. //   backgroundColor: '#000000',& j4 D" e2 ^" e- Z8 ?
  21. //   hasShineEffect: true,. [9 `* \. g8 m9 Q
  22. //   isActive: true,8 N7 _' ]) z$ ?. c
  23. //   isLoaderVisible: true,3 f' J2 Q  S/ q2 n* ?0 r2 @
  24. //   isVisible: true,% l9 P/ r: P, G, m
  25. //   text: 'My text',
    6 t$ _: v; v2 B% B" G
  26. //   textColor: '#ffffff'
    0 [! M  X$ O7 X6 Q' Z4 E
  27. // }
复制代码

Functions :

  1. import {4 a, G  j- D  p( e8 Y  R
  2.   setMainButtonParams,
    . i" B" Y( ~: C# e
  3.   mainButtonBackgroundColor,
    + ~$ D' W3 t: T& p" x( |' G
  4.   mainButtonHasShineEffect,
    # ]9 Z" ?" f# E! J, ~
  5.   isMainButtonVisible,' I* h' ~# c! r% E) w, q
  6.   isMainButtonEnabled,
    ! D( z& i( n2 \. \5 v7 O3 m3 O0 C1 @" C
  7.   isMainButtonLoaderVisible,2 Y7 c  r: R1 C3 X
  8.   mainButtonState,
    . H* }+ ~7 V2 e1 M. j& K) R
  9.   mainButtonTextColor,1 \8 j( b) G; q' ~  ?
  10.   mainButtonText,
    3 Q* }5 w1 C$ A# d0 C
  11. } from '@telegram-apps/sdk';% v3 C- @. i/ R2 O& a: X8 [. o3 E

  12. . _5 L; D; J( F5 `$ F0 x. q
  13. setMainButtonParams({
    - D2 l. z3 ?% g% F
  14.   backgroundColor: '#000000',
    # J! Z+ d- I7 T% ?% b$ Y! N  B, r9 f1 N
  15.   hasShineEffect: true,
    9 ]* d: n0 y4 d- v
  16.   isEnabled: true,
    - R, n1 r6 h8 t9 x, Q1 [; e
  17.   isLoaderVisible: true,
    9 D, c2 m- F# U3 m
  18.   isVisible: true,
    " \! i0 c% l# }" L
  19.   text: 'My text',
    ! N$ }, W9 H3 H4 o+ D  O
  20.   textColor: '#ffffff'  h, R' C0 a* u
  21. });2 A3 j+ L; Y$ f5 d, P
  22. mainButtonBackgroundColor(); // '#000000': C0 x: l' p8 S6 @8 a
  23. mainButtonHasShineEffect(); // true; h9 ]% j6 @7 ~& e9 N, Y
  24. isMainButtonEnabled(); // true
    ' j4 V  v$ o% ]7 I; Z' ~% x3 l1 a- _
  25. isMainButtonLoaderVisible(); // true
    ' C* m, n- `+ E' X5 h3 q# u
  26. isMainButtonVisible(); // true  K8 {6 p' W* w( y* B- S
  27. mainButtonText(); // 'My text'3 V  N0 J3 C! c! J/ ?
  28. mainButtonTextColor(); // '#ffffff'! m( F3 h% u( E
  29. 0 A% P6 y) X) R) M9 A4 d
  30. mainButtonState();+ P9 z: t9 N, ]( z6 T' e5 z
  31. // {
    ) Y3 Y, {. x, p5 ~
  32. //   backgroundColor: '#000000',
    ; Z" O! @- L+ y. s9 U4 f
  33. //   hasShineEffect: true,
    4 |4 P, A0 e% l+ Y$ y$ C8 J: |
  34. //   isActive: true,
    9 \. c: K2 T3 d+ ?% o- E
  35. //   isLoaderVisible: true,
    * N5 h& @) ?1 L$ Y7 B/ {# P8 Y
  36. //   isVisible: true,
    / d5 _0 O! O( A& T0 ]; E
  37. //   text: 'My text',
    : o. L0 c/ t& x& h$ H1 D
  38. //   textColor: '#ffffff'! o7 s  }4 U+ t1 a5 Y. e" D
  39. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {0 q6 X) ?9 S( N7 q! `
  2.   console.log('Clicked!');
    " _4 I" x) N. L& e8 b4 h5 W6 x( L: I
  3. }  n3 [1 l5 }& ^7 `4 h: f

  4. * ?% P  {: W: @7 ^: r6 E
  5. const offClick = mainButton.onClick(listener);  d; A4 V8 \' Q  D; O7 B, T$ W
  6. offClick();/ J' f( `2 x; c" b9 N. w5 k3 s! \6 V
  7. // or
    ! d$ ?6 ^* m8 @- j
  8. mainButton.onClick(listener);
    * X' c' t1 X7 T- y+ k
  9. mainButton.offClick(listener);
复制代码

Functions :

  1. import {
    ( R* x3 W9 W+ ^6 W. n6 @
  2.   onMainButtonClick,( f& n  j2 Z) u# u% J: F4 r
  3.   offMainButtonClick,
    + ]7 n. A5 v! Y5 z0 q
  4. } from '@telegram-apps/sdk';3 Z- w5 `* u. C

  5. 0 l5 b7 e4 W; K5 k6 T' C
  6. function listener() {# L/ e. m& Y- F
  7.   console.log('Clicked!');" t4 S$ j. a& Z. q# [+ B
  8. }
    / I$ d, k# I' f# \
  9. ) j1 h+ f& n. k3 }3 l9 e' M
  10. const offClick = onMainButtonClick(listener);  F$ P; t0 @; Y
  11. offClick();
    9 Z3 L4 }& }" p) g
  12. // or
    ! S  D# d- k- y3 n
  13. onMainButtonClick(listener);
    , U/ s" y, e* O, a8 j1 O: Y
  14. offMainButtonClick(listener);
复制代码

6 L& p# S' y* @1 r6 Q$ c: ]1 R" q3 A  t4 ?4 e) U% D
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则