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

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

回答

收藏

Telegram 小程序 | TypeScript @telegram-apps/sdk @2.x | 初始化

开源社区 开源社区 9621 人阅读 | 0 人回复 | 2025-02-24

初始化

正如 @telegram-apps/bridge 文档 中所述,必须手动定义 事件处理程序,才能开始监听 Telegram 迷你应用程序事件。

此外,在我们的代码中,我们希望确保在调用某个方法时,该方法要么有效,要么我们 会收到该方法无效的通知。 为此,有必要了解当前使用的 Telegram Mini Apps 版本。

为满足这些要求,软件包提供了 init 函数,该函数接受具有以下结构的对象 :

  1. interface Options {8 }9 m0 g8 |/ a" I, [5 n2 a
  2.   /**; d( t+ r! |! z) Y! Y" p, i% V8 Q
  3.    * True if the SDK should accept styles sent from the Telegram5 f' s1 K! H6 Y: [0 }; |
  4.    * application.
    # h3 w7 e! \, j+ h. x8 t
  5.    * @default true; B$ d9 Z. N4 M) A
  6.    */
    4 B+ p3 {3 R- l8 f+ q. ^
  7.   acceptCustomStyles?: boolean;* D% p+ V* A2 o  @" w
  8.   /**% _+ A) K. ~' G* Y! c6 w+ c9 n1 s: d
  9.    * The maximum supported Mini Apps version.* F- Z! M; z% p! l
  10.    * @default Extracted using the `retrieveLaunchParams`$ t4 V# d+ y" A4 p3 C- ?# k9 t% U
  11.    * function.
    " B( c! V$ W: d1 P, q- @
  12.    * @see retrieveLaunchParams
    6 E* T; |8 w9 S; M- A& a! ^5 r( b& ]: m$ ]
  13.    */; h5 ^" p+ J! O8 i8 i
  14.   version?: Version;
    0 r. d# Z- @6 j
  15.   /**
    2 h5 D8 T2 E& |2 g( M% Y0 M
  16.    * Custom postEvent function.
    7 v9 Z8 M, v/ v, e' V$ [
  17.    *
    . k% h8 [# {" K/ B& k- [) k
  18.    * Passing the "strict" value creates a function that always! u( g/ Q0 m8 U) j2 Y: A: c' H
  19.    * checks if the specified call is supported by the current Mini4 S9 i+ ?# R, A8 s, `4 E9 s
  20.    * Apps version. If the method is unsupported, an error$ k1 Q; N4 U& v0 U* M) I
  21.    * will be thrown.
    , Q% d( |9 G: g5 d4 V
  22.    *
    2 C9 z8 o& R. O0 g9 K
  23.    * Passing the "non-strict" value creates a postEvent function
    + ?% O) y' [$ I! i
  24.    * that doesn't throw errors but warns about missing method$ Q5 Y  ?6 N: V3 w, @, _
  25.    * support.
    4 p: ^! ~4 t4 o5 `
  26.    *
    / {+ L8 D) u) `
  27.    * @default 'strict'
    ; [( c, Q6 _4 `0 |9 L0 l) `
  28.    * @see createPostEvent3 |* o$ E6 j+ r0 A" G
  29.    */
      @3 C! s3 O4 Z& K
  30.   postEvent?: PostEventFn | 'strict' | 'non-strict';) i+ N5 }3 ~+ O3 d: b* P
  31. }
复制代码

在大多数情况下,开发人员不需要使用这些选项。

调用该函数后,将配置软件包的全局依赖关系,并创建 Telegram Mini Apps 事件处理程序。

  1. import { init } from '@telegram-apps/sdk';' v" L- o& L0 i8 \5 I% A
  2. 0 \1 Z, f( q: Y) p5 K# C( r( n
  3. init();
复制代码

: V0 U# f4 |+ V, T- ~0 }
: N  \5 W4 U2 v" w$ c/ f
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则