迷你应用程序 💠组件 负责管理 Telegram 迷你应用程序内的功能。 安装在使用该组件之前,必须使用 mount 方法安装该组件,该方法会更新 isMounted 信号属性。 Variable : - import { miniApp } from '@telegram-apps/sdk';
$ R6 U M2 X% S% g& Q9 f
1 G+ `& ^. P6 D, ~" O- miniApp.mount();4 r+ R/ i# w& i- p4 A
- miniApp.isMounted(); // true
复制代码Functions : - import { mountMiniApp, isMiniAppMounted } from '@telegram-apps/sdk';
6 q7 ?) G+ V; T' h% u3 v; p3 E - R( A# x2 V, I+ a, h l8 c* n( `
- mountMiniApp();- q% T1 q2 p( u1 {( [% `/ x
- isMiniAppMounted(); // true
复制代码INFO mount方法还会挂载 Theme Params作用域,以提取正确的 配置值。 5 L ?! D. W% f6 J+ ^! Z
要卸载组件,请使用 unmount 方法: Variable : - miniApp.unmount();
) \3 Q' j2 s4 c$ N( \+ p( d' _ - miniApp.isMounted(); // false
复制代码 Functions :
6 E- d0 O7 C3 |, R# s; m1 o
$ x+ U4 N+ I x1 H- import { unmountMiniApp, isMiniAppMounted } from '@telegram-apps/sdk';- |0 F& d9 x0 u$ _9 T- |
- 6 h- t% E; ?9 M. L( G2 {4 @
- unmountMiniApp();
' K- C* D: Q$ b5 ~ B { - isMiniAppMounted(); // false
复制代码 绑定 CSS 变量要通过 CSS 变量公开 miniApp 属性,请使用 bindCssVars 方法。 调用该方法后,isCssVarsBound 信号属性会被更新。 此方法可选择接受一个函数,该函数可将 bgColor 和 headerColor 的值转换为 CSS 变量名。 默认情况下,数值会以前缀 --tg- 转换为 kebab 大小写。 Variable : - miniApp.bindCssVars();
7 \) ?! \6 K# h1 [# r' w' x - // Creates CSS variables like:/ D( N: V% ?& m+ K: N
- // --tg-bg-color: #aabbcc) d8 R1 G4 g5 M5 |% y
- // --tg-header-color: #aabbcc% P6 V# a3 {* b8 S6 L
' j; ^9 V0 |+ k% ~& Q- miniApp.bindCssVars(key => `--my-prefix-${key}`);# {1 t( J3 R; H- r0 I
- // Creates CSS variables like:7 q. K' `! E5 N5 x) M
- // --my-prefix-bgColor: #aabbcc6 J' [( e/ o: y2 u' [
- // --my-prefix-headerColor: #aabbcc
* T. ~, j5 O5 ]) }: Q- t7 i# q) R; L* v
. v- I4 I( i$ r- Q) J- miniApp.isCssVarsBound(); // true
复制代码Functions : - import { bindMiniAppCssVars, isMiniAppCssVarsBound } from '@telegram-apps/sdk';
3 U! A5 f! P2 ~ f$ j
$ e+ H, j d! t- b% R r1 A( P" Y- bindMiniAppCssVars();( u8 j9 U/ |+ L" r
- // Creates CSS variables like:. j# {/ D* g; ^6 O8 g$ Q9 P2 p
- // --tg-bg-color: #aabbcc' C0 r! c: d. G/ T2 ?; x4 U9 w
- // --tg-header-color: #aabbcc! C3 _+ z/ {1 [8 d8 G2 m2 w
- / P* [; f$ G) y2 M
- bindMiniAppCssVars(key => `--my-prefix-${key}`);0 M Z- M7 r) l) W
- // Creates CSS variables like:
$ f+ e s# H+ k% X2 i5 p# \8 ?, g - // --my-prefix-bgColor: #aabbcc) J* a3 \9 r- f" S
- // --my-prefix-headerColor: #aabbcc8 x2 e! ^4 }1 g3 ]1 M8 L6 g
- ( A& s" C8 K. [3 l$ ]
- isMiniAppCssVarsBound(); // true
复制代码 页眉颜色要更改迷你应用程序的标题颜色,可使用方法 setHeaderColor。 反过来, 会更新 headerColor 信号属性值。 该方法接受 RGB 颜色值或以下字符串之一:bg_color、secondary_bg_color。 Variable : - if (miniApp.setHeaderColor.isSupported()) {/ M4 S% v; y. x' s1 I# e7 t8 N8 p* L0 b
- miniApp.setHeaderColor('bg_color');
- ]% Z* y" u4 Z7 m/ Q - miniApp.headerColor(); // 'bg_color'
. d2 a3 L4 F& z7 D/ J# j% \/ p - }, _* S; _6 h9 H1 f# ^
6 X4 R0 I4 f6 t0 q- if (
' L' O# G' y$ J7 m# K* z2 e - miniApp.setHeaderColor.isSupported()
1 P2 n6 O+ w. x7 y2 t4 Y* M - && miniApp.setHeaderColor.supports('color')
! g6 G; i6 |# X4 t - ) {
& `8 F' r! l1 a" [ - miniApp.setHeaderColor('#aabbcc');
9 ~; Z5 _. X J# M) g. F) v/ T5 {& | - miniApp.headerColor(); // '#aabbcc'$ w) V2 m F+ X5 }) ~
- }
复制代码Functions : - import {: r! l# T! g7 i" a1 ?: T$ P- \
- setMiniAppHeaderColor,' h' T7 \/ n% _/ C0 A* S& r
- miniAppHeaderColor,( v+ m5 C4 U" [! F0 H, K, Z
- } from '@telegram-apps/sdk';1 k1 D* v3 T" E: p3 @0 K
- , g8 m" m' y$ m
- if (setMiniAppHeaderColor.isSupported()) {
, i7 I& g, v) ~, P; w" p$ L9 T2 K4 m - setMiniAppHeaderColor('bg_color');3 X" _5 Q- v1 B2 {- [
- miniAppHeaderColor(); // 'bg_color': m D5 T) Q- a5 f1 g( X/ h! u
- }
* c3 [! R9 i3 C) c3 e - * Y5 P# N; y" m. T3 y l. b
- if (
. l. n% N( [+ P) H6 j- E - setMiniAppHeaderColor.isSupported()
4 R6 ]/ Y' {% ?8 _5 ` - && setMiniAppHeaderColor.supports('color'). a/ N4 d0 ~! C* V* W& l* B
- ) {! I! C, }1 c' Q+ S
- setMiniAppHeaderColor('#aabbcc');
: i1 f; |( N" c: {/ |, G# j - miniAppHeaderColor(); // '#aabbcc') R& F/ A, p# F2 F" y
- }
复制代码 背景颜色要更新迷你应用程序的背景颜色,请使用 setBackgroundColor 方法。 调用 时,该方法会更新 headerColor 信号属性值。 Variable : - if (miniApp.setBackgroundColor.isSupported()) {3 j. u7 \% X+ R# U Z# Q$ C
- miniApp.setBackgroundColor('#ffffff');
0 R9 r3 M2 v1 e) f+ @ - miniApp.backgroundColor(); // '#ffffff'$ ?5 T! c7 p- K$ I
- }
复制代码Functions : - import { , \( l' z! B# ^. a) W3 O1 c
- setMiniAppBackgroundColor,8 [" C' X+ A. U) Y8 _
- miniAppBackgroundColor," u) _' r) Z" F3 x1 J% H# w' C( H+ f
- } from '@telegram-apps/sdk';
. K6 A3 r" m, V( E
% L8 \3 T( T! }2 a! T5 @- if (setMiniAppBackgroundColor.isSupported()) {
+ @1 _/ W0 z* \' C h5 a - setMiniAppBackgroundColor('#ffffff');
' z' h0 ~* n) _' q1 `8 ^) ~ - miniAppBackgroundColor(); // '#ffffff'; z8 l' v( z. B! Z, G x9 c7 i" E; o
- }
复制代码 方法close要关闭迷你应用程序,请使用 close 方法。 Variable : Functions : - import { closeMiniApp } from '@telegram-apps/sdk';1 F- j9 G) Z5 z" w- I2 k0 A# S" U4 F
2 o4 Y( L& l1 T3 ~) ?6 M! Q- closeMiniApp();
复制代码 ready要发出 Mini App 已准备好显示的信号,请使用 ready 方法。 调用后, 加载占位符会被隐藏,而迷你应用程序则会显示出来。 Variable : Functions : - import { miniAppReady } from '@telegram-apps/sdk';9 k* [. M: `7 U; o; }' \8 ^
- ; C( S6 o0 `1 C: ]+ s& L9 r' k
- miniAppReady();
复制代码TIP 在加载基本界面元素后尽早调用该函数,以确保 顺畅的用户体验。 , w% @- i' v- h5 }1 U' K
|