迷你应用程序 💠组件 负责管理 Telegram 迷你应用程序内的功能。 安装在使用该组件之前,必须使用 mount 方法安装该组件,该方法会更新 isMounted 信号属性。 Variable : - import { miniApp } from '@telegram-apps/sdk';
" J+ u& v. U) v* r9 V: s - & p* v. j; s- N$ P- F; x) {0 l
- miniApp.mount();
& T4 D: K6 x3 S7 C p4 u - miniApp.isMounted(); // true
复制代码Functions : - import { mountMiniApp, isMiniAppMounted } from '@telegram-apps/sdk';6 u6 N3 G4 o+ `
- 8 Z6 s6 Y* x" ^$ P0 F$ z
- mountMiniApp();
( g/ c7 P1 }* w X5 m; D+ b - isMiniAppMounted(); // true
复制代码INFO mount方法还会挂载 Theme Params作用域,以提取正确的 配置值。 . G1 T, ^8 T7 i
要卸载组件,请使用 unmount 方法: Variable : - miniApp.unmount();
! f4 A7 _, V" ^: B) o, ~; ? - miniApp.isMounted(); // false
复制代码 Functions :, |) E2 `" Q9 U; O+ K
' B6 A6 H6 U" K8 n$ b' @) v
- import { unmountMiniApp, isMiniAppMounted } from '@telegram-apps/sdk';
+ Q6 J; k; i2 O( @ W; B$ O* U
" c) Y3 c: G, ?% g% ?- unmountMiniApp();5 \* {) c; ~* h' ~. ^
- isMiniAppMounted(); // false
复制代码 绑定 CSS 变量要通过 CSS 变量公开 miniApp 属性,请使用 bindCssVars 方法。 调用该方法后,isCssVarsBound 信号属性会被更新。 此方法可选择接受一个函数,该函数可将 bgColor 和 headerColor 的值转换为 CSS 变量名。 默认情况下,数值会以前缀 --tg- 转换为 kebab 大小写。 Variable : - miniApp.bindCssVars();/ {9 o* b: J$ b/ S
- // Creates CSS variables like:, ^6 L& T$ e9 ], r4 j8 s b5 a
- // --tg-bg-color: #aabbcc# o m; T- ~$ l
- // --tg-header-color: #aabbcc
5 H, F, F; X/ N+ ?. v6 i# s. ]" [ - 0 ^7 o8 R; k9 M* t
- miniApp.bindCssVars(key => `--my-prefix-${key}`);2 {2 e. d8 _+ f+ V; o/ P7 E' r/ O
- // Creates CSS variables like:. l y9 D' ]# F. G
- // --my-prefix-bgColor: #aabbcc
! B& C& ]0 Q& x i- B$ X% P( I - // --my-prefix-headerColor: #aabbcc
+ I) Q- O5 W4 X
; [' O" q! y7 @- miniApp.isCssVarsBound(); // true
复制代码Functions : - import { bindMiniAppCssVars, isMiniAppCssVarsBound } from '@telegram-apps/sdk';
9 \4 `) u* L% l+ t$ d
3 @% f" n: J T; N7 |0 E0 w) k9 l2 E- bindMiniAppCssVars();
6 K3 z" {- w$ d5 D$ W1 x. T - // Creates CSS variables like:& g: n0 ?! s" U' a. h( v0 ~
- // --tg-bg-color: #aabbcc* i7 V3 Z2 b6 N" U3 n
- // --tg-header-color: #aabbcc1 W9 Y( Q3 @0 \) B
- 5 C2 \1 C! {% u t
- bindMiniAppCssVars(key => `--my-prefix-${key}`);
7 m \9 z. u, W W* g$ P6 b7 R - // Creates CSS variables like:# j f1 \7 t7 B; y' U7 z( R
- // --my-prefix-bgColor: #aabbcc0 M a3 u0 u" H% z7 V
- // --my-prefix-headerColor: #aabbcc& \: X9 T# u, _/ y
) E0 }* M l, P, Y2 k+ B) {5 g2 ]- isMiniAppCssVarsBound(); // true
复制代码 页眉颜色要更改迷你应用程序的标题颜色,可使用方法 setHeaderColor。 反过来, 会更新 headerColor 信号属性值。 该方法接受 RGB 颜色值或以下字符串之一:bg_color、secondary_bg_color。 Variable : - if (miniApp.setHeaderColor.isSupported()) {: X" r* O8 ~/ I+ U ?, n7 d
- miniApp.setHeaderColor('bg_color');: Z* ]5 U( b) ? A# S+ l0 p
- miniApp.headerColor(); // 'bg_color'
+ ~/ l5 q) }8 n: q - }
3 E- }1 u* ~( G+ ~- g9 Q% n& t: i- R
1 p6 `. U$ v+ w4 ]9 v: @ x& Z! U- if (; Q4 ~3 ]1 V% U0 Q
- miniApp.setHeaderColor.isSupported()5 q" l) }8 W. P9 B; m* e o' A; M, f
- && miniApp.setHeaderColor.supports('color')5 U: [; T* i! G
- ) {9 z/ p, i" J- v9 P g
- miniApp.setHeaderColor('#aabbcc');6 Q# T+ z V- _& x; j! b \0 n
- miniApp.headerColor(); // '#aabbcc'
/ z) T& L ^/ f+ b& C) a - }
复制代码Functions : - import {2 m; N3 }. d. q( }/ Y* Z
- setMiniAppHeaderColor,
% y$ y; \ D7 [2 P; e' Y' Z - miniAppHeaderColor,4 W; I$ y d# m" [
- } from '@telegram-apps/sdk';) Z. g6 X0 A9 M0 \- [" P" a
- / K# Q% A* D& X- [. L2 j; S+ s+ y$ o
- if (setMiniAppHeaderColor.isSupported()) {4 j% w$ q% u1 }: v6 K& m2 x
- setMiniAppHeaderColor('bg_color');$ N! ]: \- P6 g7 H! l' N
- miniAppHeaderColor(); // 'bg_color'. \; v6 n" S# K1 Y3 r6 K! Z" C
- }. {, M7 r) u$ ]8 _% l- P# k v- \
- 3 l; F, w, P4 V; _) `: D, t1 ~
- if (+ M; E6 _ l! K. T" c% B1 ?
- setMiniAppHeaderColor.isSupported()
$ z5 n! I0 Y5 a$ x/ U - && setMiniAppHeaderColor.supports('color')! D) B4 P& _; A. [
- ) {% t) l6 }6 c. e( X( K& m) }+ |
- setMiniAppHeaderColor('#aabbcc');& I m+ K/ ^* @% p8 k
- miniAppHeaderColor(); // '#aabbcc'
. y. m! N" Y/ r& R - }
复制代码 背景颜色要更新迷你应用程序的背景颜色,请使用 setBackgroundColor 方法。 调用 时,该方法会更新 headerColor 信号属性值。 Variable : - if (miniApp.setBackgroundColor.isSupported()) {
( Y; a4 v$ q# j" u4 V - miniApp.setBackgroundColor('#ffffff');
, e) A' g/ ~- |+ y+ E( ` - miniApp.backgroundColor(); // '#ffffff'
8 `5 U r9 e; I8 x% J* n" e - }
复制代码Functions : - import {
! c" ]" c, V6 V3 M6 _5 { w; n - setMiniAppBackgroundColor,7 P# w* w! s% u2 Q. l
- miniAppBackgroundColor,
# e6 V& M7 u6 c8 I& I; K/ d1 i - } from '@telegram-apps/sdk';
7 {; ?; l: `! Q3 _$ j
, v5 I! L$ z7 d- ^) o2 d8 L/ }3 k- if (setMiniAppBackgroundColor.isSupported()) {
. O7 y& M M. H. a! a - setMiniAppBackgroundColor('#ffffff');$ g# G) v3 G- k3 b& m2 Q+ t
- miniAppBackgroundColor(); // '#ffffff'1 n7 b1 h S. I, C& h
- }
复制代码 方法close要关闭迷你应用程序,请使用 close 方法。 Variable : Functions : - import { closeMiniApp } from '@telegram-apps/sdk';5 Z0 W7 P, ]* n3 E! S
- ( t% R, O6 [) I5 }( G. ^* A
- closeMiniApp();
复制代码 ready要发出 Mini App 已准备好显示的信号,请使用 ready 方法。 调用后, 加载占位符会被隐藏,而迷你应用程序则会显示出来。 Variable : Functions : - import { miniAppReady } from '@telegram-apps/sdk';
# N) N! V1 m+ q
0 H/ s0 u5 W3 Z3 O, t; H# x- miniAppReady();
复制代码TIP 在加载基本界面元素后尽早调用该函数,以确保 顺畅的用户体验。 , l5 y, f! m7 |: K9 _
|