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

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

回答

收藏

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

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

初始化

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

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

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

  1. interface Options {
    " K+ g+ `9 J* F
  2.   /**3 Y9 l+ U9 k) `3 h5 e
  3.    * True if the SDK should accept styles sent from the Telegram; @! r! ]4 I; w; e/ ]: i7 x, E2 G
  4.    * application.
    - j1 s2 j0 u, ^( @% q4 a) p
  5.    * @default true& D4 i$ l- V- B. q1 Z+ `2 c4 A
  6.    */
    3 e. e  }0 g* j3 S& j3 i
  7.   acceptCustomStyles?: boolean;
    5 s4 q  a) M9 d
  8.   /**
    $ u: V* {4 [- N) H
  9.    * The maximum supported Mini Apps version.+ Y8 F% z9 K. Q% X9 l, I
  10.    * @default Extracted using the `retrieveLaunchParams`
    2 h6 V0 P) G. ]' Z* D
  11.    * function." V" P' A+ y! K( n
  12.    * @see retrieveLaunchParams
    : o. Z( h% v7 O
  13.    */
    4 \2 a4 C7 n  Q; m$ S
  14.   version?: Version;
    8 K8 ^# ~; z/ N' k  `
  15.   /**
    7 M* L8 @8 [& j, w) ?% J
  16.    * Custom postEvent function.
    , a1 m* b% {2 H9 `6 {' r/ }3 s
  17.    *- J3 z1 ~8 z1 F! n" l( {# ^0 v
  18.    * Passing the "strict" value creates a function that always
    : O  {1 d5 j% L
  19.    * checks if the specified call is supported by the current Mini
    1 j+ S1 S8 l: P( a' _  h8 J
  20.    * Apps version. If the method is unsupported, an error
    1 Z0 G; M  V+ P/ c2 S4 v$ ?5 b: g& j
  21.    * will be thrown.7 j$ G9 g5 b, L. c, e7 j
  22.    *
    3 C$ v0 g+ P# @  Z: @4 y9 x
  23.    * Passing the "non-strict" value creates a postEvent function
    0 ]! n# q; ]  N* O; T+ b
  24.    * that doesn't throw errors but warns about missing method
    ! K+ @: t( [, `4 K2 f. g
  25.    * support.
    1 Z0 S5 }, x: `! b
  26.    *
    . y, ]) [0 H: {
  27.    * @default 'strict'
    3 S! M& p+ N& T. t! E) G
  28.    * @see createPostEvent
    1 b# |! @" _- A$ s# v$ C; b
  29.    */
    9 b, W$ E& P- N( ^; [
  30.   postEvent?: PostEventFn | 'strict' | 'non-strict';
    0 x& s  [2 U9 Z- g' {& M. A8 Q
  31. }
复制代码

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

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

  1. import { init } from '@telegram-apps/sdk';. k% i; Q& ~( c

  2. # g/ A; d# @8 b: l
  3. init();
复制代码
) p2 L) g, b* u$ \( d9 y

! S5 b* V# R& j- }, t. Q; Z
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则