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

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

回答

收藏

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

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

主按钮

负责 Telegram Mini Apps main button 的💠组件

安装

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

Variable :

  1. import { mainButton } from '@telegram-apps/sdk';) p: i$ [4 \% E% U1 N% M/ g! f3 T1 t
  2. 2 Q# C7 I0 m/ N9 n, j
  3. mainButton.mount();
    : A7 }$ F! u# w) G9 X  \
  4. mainButton.isMounted(); // true
复制代码

Functions :

  1. import { mountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';% G' s4 |9 I, r# ^. l9 e( @8 R( l
  2. ) i  ]) i+ U+ ?$ l4 ~4 i/ ?( M/ Z3 _! e
  3. mountMainButton();
    ' A0 U. o6 j6 |) T: i7 r: I! W
  4. isMainButtonMounted(); // true
复制代码
4 Y! W8 L! b" C; R- @$ }

要卸载,请使用 unmount 方法:

Variable :

  1. mainButton.unmount();
    6 ^* o* a' Y8 V/ ?& a9 \4 t7 Y
  2. mainButton.isMounted(); // false
复制代码

Functions :

  1. import { unmountMainButton, isMainButtonMounted } from '@telegram-apps/sdk';
    ( z/ B1 q8 A" U0 m) ~4 d& p
  2. * @/ p1 k! c, K) y9 t: c4 q% l
  3. unmountMainButton();
      G5 l. e% l. `- d0 s0 T
  4. isMainButtonMounted(); // false
复制代码

WARNING

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

设置属性

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

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

Variable :

  1. mainButton.setParams({
    & e5 w. a9 t" u5 l7 y4 S
  2.   backgroundColor: '#000000',
    6 Z/ B+ X5 j* |. H7 f: v
  3.   hasShineEffect: true,; A' I# H7 l6 s# B; f
  4.   isEnabled: true,
    " h& p9 k' k8 w4 `5 w: S7 p, y7 [6 \
  5.   isLoaderVisible: true,8 B. _$ i( _: M6 P
  6.   isVisible: true,+ ]4 z; z) `8 d4 v/ D7 j6 c
  7.   text: 'My text',
    ) B/ y) c: I2 P) q) F' W
  8.   textColor: '#ffffff'; r1 h- e" ?) N% ?, s1 \' u2 ?
  9. });
    0 a. z4 d, n$ t- q
  10. mainButton.backgroundColor(); // '#000000'
    % k9 i3 I2 o9 Y! a7 K
  11. mainButton.hasShineEffect(); // true. b5 V  M" l: i" c6 L7 |, c% H
  12. mainButton.isEnabled(); // true. H) j) l8 F4 [8 u( D% b. v/ x; z0 {
  13. mainButton.isLoaderVisible(); // true
    : w! s/ c7 T/ t( t
  14. mainButton.isVisible(); // true
    ( d/ [/ R+ E& v, R, }: t
  15. mainButton.text(); // 'My text'; A0 I- m/ u/ B9 k' @* u
  16. mainButton.textColor(); // '#ffffff'
    ) B! ]  L9 ]6 {" d3 L$ [( C1 x
  17. " z4 d0 e6 u6 k& g( d2 M0 g
  18. mainButton.state();
    0 s5 i4 _: q. T8 Y2 x3 G$ F* j3 l
  19. // {0 U8 u6 Q+ b% d2 a
  20. //   backgroundColor: '#000000',/ h. ~; o, a8 x. T) r
  21. //   hasShineEffect: true,
    3 [8 K8 C$ l0 q& j4 z+ w
  22. //   isActive: true,5 y) c* F/ u# p% x( m% a5 H
  23. //   isLoaderVisible: true,8 x/ b! _- p; w/ R  A+ A0 C3 B
  24. //   isVisible: true,
    4 L% m4 b9 F+ k8 E+ M; }# n
  25. //   text: 'My text',9 F" l$ X) S% D$ j# @# V2 H$ z
  26. //   textColor: '#ffffff'- Q6 M6 z" w5 E' L
  27. // }
复制代码

Functions :

  1. import {
    ! H9 |. w0 |% V1 q: S+ B' G
  2.   setMainButtonParams,
    . A' S7 m( E+ }
  3.   mainButtonBackgroundColor,4 ~" ^9 L6 l& ~0 \" P* r8 I
  4.   mainButtonHasShineEffect,; e  M( `) [8 e/ \7 r
  5.   isMainButtonVisible,
    " g% K5 M( q9 f  K
  6.   isMainButtonEnabled,1 e! l( l& L! d5 b, J! F
  7.   isMainButtonLoaderVisible,9 b  ^; G7 J  q0 K. Z! F6 D! U
  8.   mainButtonState,
    ; F9 C+ ]4 d/ z. c) ?
  9.   mainButtonTextColor,
    8 N6 j4 o4 G+ p- Y+ p
  10.   mainButtonText,; `* ^( E6 g2 a& t5 e
  11. } from '@telegram-apps/sdk';
    9 L( y  o4 r) C- w( K7 F
  12.   M& Y6 d, |, j
  13. setMainButtonParams({! R) {8 h( ~& C4 _7 d1 ]7 `
  14.   backgroundColor: '#000000',
    + D+ ?2 w* [, d1 v7 ?( Q
  15.   hasShineEffect: true,
    ( ^+ N9 i- r) g3 q
  16.   isEnabled: true,
    $ J7 e0 M3 F7 S' i& n
  17.   isLoaderVisible: true,2 {5 N" Z7 m# [" H8 V/ ~  P& O% J
  18.   isVisible: true,$ P/ d3 c; O4 `! h9 z8 m9 n. U5 |
  19.   text: 'My text',6 s. v5 M' I2 _6 y! b# ~: c/ r
  20.   textColor: '#ffffff') Q4 E% Y5 m( b, g" y
  21. });
    . l4 {$ U1 M/ v9 a& ?4 F6 D. ?, @
  22. mainButtonBackgroundColor(); // '#000000'2 M" P# t# D8 r' h  }1 R
  23. mainButtonHasShineEffect(); // true- a* Z. t7 I2 f/ g
  24. isMainButtonEnabled(); // true! M3 y: Y! V% u: K% d
  25. isMainButtonLoaderVisible(); // true+ |- U, E, k# p+ \$ b! w, X0 d" X
  26. isMainButtonVisible(); // true
    6 i: P5 Q# p- E( N% w1 t3 n+ J# @
  27. mainButtonText(); // 'My text'5 X4 x; R; P+ m2 n' \6 r+ \
  28. mainButtonTextColor(); // '#ffffff'
    - I) ]9 v, @4 R8 [9 r7 o

  29. . s8 B" t8 k0 U8 i/ i3 Y
  30. mainButtonState();( X& F3 k2 L4 M5 x+ q7 q# N
  31. // {+ m: P! b( v" S
  32. //   backgroundColor: '#000000',
    ' n7 T- i3 |" `; k
  33. //   hasShineEffect: true,
    , R, `2 ?. p% E7 B9 i
  34. //   isActive: true,
    2 x% ~6 I  T# f& P2 {1 x
  35. //   isLoaderVisible: true,
    ; S$ W: {2 O+ G
  36. //   isVisible: true,
    ) i7 s- A) {8 D
  37. //   text: 'My text',) Y3 W* n4 h/ d% ^
  38. //   textColor: '#ffffff'
    5 @9 o4 o+ }8 r
  39. // }
复制代码
跟踪点击

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

Variable :

  1. function listener() {
    $ h9 o3 `5 u0 O4 }
  2.   console.log('Clicked!');
    4 e0 O4 r% p8 C( v' a) Q
  3. }
    - Z- P+ j1 o7 W' P( A
  4. + _+ h1 |- L" P( A  z: @7 x
  5. const offClick = mainButton.onClick(listener);( e( C! g' S- M" X% D9 L: {
  6. offClick();
    5 _+ o  H% ?" g4 G
  7. // or
    9 g3 B5 M) D5 t! p, z- m5 l% v; a* X
  8. mainButton.onClick(listener);
    : W" ]) ?& q+ Y1 f6 ?: U+ h, s
  9. mainButton.offClick(listener);
复制代码

Functions :

  1. import {9 V( g' w# m0 h9 t% f( m* g
  2.   onMainButtonClick,
    6 S) y( c0 q$ r8 h
  3.   offMainButtonClick,# z7 ?2 N8 c4 D- L
  4. } from '@telegram-apps/sdk';
    3 Y4 z1 T& _! |1 R# Q2 h) a/ g

  5. 2 U! L2 t& O" p. V
  6. function listener() {
    - d9 |: @0 P2 l9 S0 E
  7.   console.log('Clicked!');; F0 m7 S0 y' ~
  8. }% Q6 @5 N1 i5 N2 ~9 |1 s

  9. 4 [& x& r- L$ N
  10. const offClick = onMainButtonClick(listener);1 R5 m# }& P, N9 Z: T$ W1 x9 R0 \
  11. offClick();
    . V5 i# v6 u* z! j; y# |$ {( m% H
  12. // or9 ~" M& f9 b5 U7 T( Q
  13. onMainButtonClick(listener);
    + G; u$ f5 t, d  {
  14. offMainButtonClick(listener);
复制代码
& T  K7 G4 a- H6 M6 H
4 j& C% \- r6 M2 Q( y6 E
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则