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

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

回答

收藏

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

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

主按钮

负责 Telegram Mini Apps main button 的💠组件

安装

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

Variable :

  1. import { mainButton } from '@telegram-apps/sdk';
    5 h  \9 D) E3 V  B$ H
  2. " K, ?7 ~7 v: [0 D* g! _" w
  3. mainButton.mount();
    1 A0 k! C6 T! G# o% l
  4. mainButton.isMounted(); // true
复制代码

Functions :

  1. import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';" `1 s2 y/ P; S( c2 o# M5 E
  2. 1 t6 c! X. w  K- R8 J
  3. mountMainButton();
    % D* q  [0 d/ q$ R$ J
  4. isMainButtonMounted(); // true
复制代码
) W0 u# E. _3 d7 k$ H4 G

要卸载,请使用 unmount 方法:

Variable :

  1. mainButton.unmount(); 8 M0 A: i; r) f) Q# D- ~
  2. mainButton.isMounted(); // false
复制代码

Functions :

  1. import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
    / G6 P6 {6 `  C. J; }8 }+ Q- k/ o

  2. / b" H( N" H: i- w1 E
  3. unmountMainButton();
    ) ?" p: s' B& a- f0 W6 p% r
  4. isMainButtonMounted(); // false
复制代码

WARNING

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

设置属性

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

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

Variable :

  1. mainButton.setParams({
    ' L/ X# N( {  f
  2.   backgroundColor: '#000000',
    6 y% T5 S( L5 E) j3 `
  3.   hasShineEffect: true,
    8 ]9 D7 O$ K# e' {6 x
  4.   isEnabled: true,
    . s. f; R5 w2 X0 c( }$ k5 p
  5.   isLoaderVisible: true,! A1 M. D6 ~0 P- @/ }4 _( w
  6.   isVisible: true,
    & {8 c0 U# c) b1 B2 e+ H3 F4 {$ K9 p
  7.   text: 'My text',/ O- m& X9 Q; \* Q8 E7 y
  8.   textColor: '#ffffff'
    # l4 B* z* N. [3 E5 Q8 m
  9. });
    7 L' ]9 L+ o& X# M" c# `5 {
  10. mainButton.backgroundColor(); // '#000000'
    , {. Y' G3 o0 B: K6 F
  11. mainButton.hasShineEffect(); // true
    7 ^; S2 G7 k% n% G6 Q/ g- I, Z; _, S
  12. mainButton.isEnabled(); // true" C3 I) Z2 k& R# k" Z  j. n
  13. mainButton.isLoaderVisible(); // true
    7 J  q: w; M9 ?0 }- {2 u
  14. mainButton.isVisible(); // true
    * f$ ^2 D& ?5 {
  15. mainButton.text(); // 'My text'6 L1 u6 C9 V9 C4 S% \
  16. mainButton.textColor(); // '#ffffff', V$ O: w2 p8 z) R" {

  17. ) h# m# [2 U' C+ M
  18. mainButton.state();
    ' L% ?+ }5 M9 ?+ l! }) I8 l
  19. // {8 O  k' j% F: `3 z. Z& T* N
  20. //   backgroundColor: '#000000',2 z3 L8 V6 |7 z* [$ H
  21. //   hasShineEffect: true,: n! ~( ~9 i: s% v: i) B& X' x
  22. //   isActive: true,1 b  [2 @2 |; `8 ?
  23. //   isLoaderVisible: true,
    : E$ t/ O. [! w0 z+ Y& X% r# S
  24. //   isVisible: true,
    ' ~3 a: O' [5 S
  25. //   text: 'My text',
      y/ m$ {& z: k7 g( F6 L+ V
  26. //   textColor: '#ffffff'
    * }% l& K- o# {$ o
  27. // }
复制代码

Functions :

  1. import {
    : B& j& e' ]' l; {5 S
  2.   setMainButtonParams,! u7 d/ Q( Z* q0 A
  3.   mainButtonBackgroundColor,
    5 F9 B/ r: C" r
  4.   mainButtonHasShineEffect,
    * ]0 z9 S' D5 L6 v
  5.   isMainButtonVisible,+ D: H* z$ J8 q+ o" l8 z
  6.   isMainButtonEnabled,
    : L; v+ j9 F0 d3 Y' x1 ^, u4 H5 \
  7.   isMainButtonLoaderVisible,
    ! c, x, U4 N$ E. C5 R
  8.   mainButtonState,
    7 A) N7 b/ Q5 B0 y3 m
  9.   mainButtonTextColor,
    # V- I! r6 d/ l* D7 b1 T8 I
  10.   mainButtonText,
    , J% v- w' p7 {& t; ^
  11. } from '@telegram-apps/sdk';
    # B/ t5 Y* ~- R* w" b
  12. 4 I8 {0 s! A1 n
  13. setMainButtonParams({" P* |+ x7 z" Q, y) g' E* a
  14.   backgroundColor: '#000000',5 R! T2 V1 u: F3 U7 q7 A3 T
  15.   hasShineEffect: true,
    , [; Q' _) h$ X
  16.   isEnabled: true,1 J, M* q4 X+ F7 ~4 Z, z
  17.   isLoaderVisible: true,
    , ?4 a! X: f$ H9 l) U
  18.   isVisible: true,5 Y) i: Q% \/ g- f' _) u: Y4 s
  19.   text: 'My text',
    * A( y6 h4 O. v; M: l& ~+ O7 t
  20.   textColor: '#ffffff'
    - t* C2 I! K# I9 p+ z' r
  21. });
    ' l2 F1 O1 F* A  k, |" P1 \
  22. mainButtonBackgroundColor(); // '#000000'
    2 e% c, l: }: u' ]) b6 ~
  23. mainButtonHasShineEffect(); // true9 z/ M8 D5 M9 W; ]# N9 b2 m3 R
  24. isMainButtonEnabled(); // true) J6 J7 w5 J9 J/ K
  25. isMainButtonLoaderVisible(); // true
    , N; \. g! s5 a* h0 T. ?8 B" a
  26. isMainButtonVisible(); // true" N. s: i% n7 x, ]4 C
  27. mainButtonText(); // 'My text'
    3 i* S2 A$ t8 |2 b
  28. mainButtonTextColor(); // '#ffffff'5 [9 v# [3 |7 P6 J/ \5 d

  29. 4 _! p, V: L# e  U2 [
  30. mainButtonState();' E4 u( r1 V4 B0 s: _+ A# S9 g) Z
  31. // {# v9 D7 S, x8 k# P( D& ]3 ^
  32. //   backgroundColor: '#000000',2 G8 }" M6 r! ~* ~
  33. //   hasShineEffect: true,$ u$ a) }6 a1 F3 B) b
  34. //   isActive: true,3 [5 Z  O3 j- C- o7 P+ G( q
  35. //   isLoaderVisible: true,
    + v, f! O( g; d, H8 C8 ]
  36. //   isVisible: true,
    4 Z5 P* l" ~/ F' E- d% v
  37. //   text: 'My text',
    , P, {# G5 _# O, C* M
  38. //   textColor: '#ffffff'
    ' ]( r, O6 B( a* }) u5 i
  39. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {
      c, a! U/ E, v* o
  2.   console.log('Clicked!');) S) O6 K; Q8 x7 B9 X
  3. }3 V4 S! d: w3 w

  4.   z1 d( i; t7 E" \/ t+ x
  5. const offClick = mainButton.onClick(listener);6 Z# L& ~2 n, {$ O2 e
  6. offClick();, L% g( j+ P/ {: _& j
  7. // or
    ( T5 L/ y  i$ d% {" X
  8. mainButton.onClick(listener);. r( ?9 c) M( ~0 e0 @
  9. mainButton.offClick(listener);
复制代码

Functions :

  1. import {+ h$ V/ p; Z. }% U+ Y5 m, [
  2.   onMainButtonClick,: L" ~% B8 ?4 q9 R; N( d# t/ b1 b
  3.   offMainButtonClick,: x# n( L0 b( a; U2 A
  4. } from '@telegram-apps/sdk';9 S9 {; i2 `4 a" e6 B/ v& _
  5. 6 x( t& f7 Q( P1 @
  6. function listener() {
    : I0 w, m5 r- {, R: C
  7.   console.log('Clicked!');
    8 e9 \9 U* N7 q* Z: u) m
  8. }
    2 A( ^; c& v! E4 ~
  9. % @5 a- A5 ?7 [5 s! y
  10. const offClick = onMainButtonClick(listener);
    6 z* Y- [6 t+ T* o$ u8 ~6 ?6 I
  11. offClick();: P+ l. K" a6 A$ y: n3 H
  12. // or
    # ?8 V1 F" o. n* _& P
  13. onMainButtonClick(listener);
    $ s$ F& E8 Q: D' c2 A: F( J& w
  14. offMainButtonClick(listener);
复制代码

5 @8 P& f' Q/ f+ R5 d, L6 g1 [5 ^9 z! }+ j2 }# y
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则