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

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

回答

收藏

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

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

初始化

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

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

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

  1. interface Options {* d$ }, H- T0 ^7 U; U7 ]4 y
  2.   /**
    + R" }# o( [& B1 T
  3.    * True if the SDK should accept styles sent from the Telegram
    & _9 Y5 k' ?! e0 p8 r
  4.    * application.: ?8 ?" U5 K7 I% x8 u5 ^
  5.    * @default true  C; h8 x! M. r6 ]7 y0 \/ ]$ |: n
  6.    */
    2 n. o8 B9 ?6 t4 R" P& N
  7.   acceptCustomStyles?: boolean;
    : K0 q( z7 k5 G2 C4 j) f9 ^
  8.   /**
    / R: c7 t) a) m4 g
  9.    * The maximum supported Mini Apps version.; b" d" p7 Z, r1 M  w
  10.    * @default Extracted using the `retrieveLaunchParams`
      {7 J) U" h) |0 G7 u7 b4 k& J
  11.    * function.
    / y" W' W+ j9 a% ]. @* a" a4 \8 C. h
  12.    * @see retrieveLaunchParams
    4 M/ f8 k! w6 f6 k
  13.    */+ X$ w: v: _/ ?, g; }& k
  14.   version?: Version;0 p2 }; d  S; O/ t
  15.   /**
    $ M5 v5 T  p0 C' n# G' Q  e
  16.    * Custom postEvent function.
    9 b0 R. V+ ]  f/ b7 L
  17.    *
    4 R' }# M/ x3 w5 q1 @
  18.    * Passing the "strict" value creates a function that always
    - O9 a4 s9 d$ G5 U+ U# R
  19.    * checks if the specified call is supported by the current Mini
    3 ?  S; e8 x. O8 {
  20.    * Apps version. If the method is unsupported, an error
    ' g7 x  k  W& d! ~; z& `
  21.    * will be thrown.
    5 A- z; x* H; X) S. [: z
  22.    *9 Q7 R' v+ e+ {- K8 N; ?2 A& J2 ~+ z
  23.    * Passing the "non-strict" value creates a postEvent function5 h1 ^( q$ \" C% `( a% l
  24.    * that doesn't throw errors but warns about missing method3 G  Q+ ?# z8 W$ o/ C
  25.    * support.( o: V5 ~& |) J9 j- Q( F* i4 ?
  26.    *
    ; G% I% L! ^+ ]* G' ~
  27.    * @default 'strict'
    : D% a- l4 D0 ]. e
  28.    * @see createPostEvent* ?' M- g$ C; |: K" w. {8 E
  29.    */
    * V$ V) u+ N2 ^) f+ t$ o; E
  30.   postEvent?: PostEventFn | 'strict' | 'non-strict';
    1 e4 ^" D9 z4 Q# i8 T
  31. }
复制代码

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

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

  1. import { init } from '@telegram-apps/sdk';
    ; Q' C* O6 L: i% J
  2. & R5 p# J0 A: i( a/ Y
  3. init();
复制代码
; e; e$ d( Q: K! t! b

  Q- N6 p2 O4 P% A0 m) W
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则