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

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

回答

收藏

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

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

初始化

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

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

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

  1. interface Options {3 U7 D9 G' B  B
  2.   /**- _$ F( Q# r8 @( B- ^
  3.    * True if the SDK should accept styles sent from the Telegram
    / I8 n9 |; {$ _( W
  4.    * application.2 o( H8 t& ^0 d" W
  5.    * @default true
    ) m7 B# a; g9 a
  6.    */
    8 u0 p' M) }& a' \
  7.   acceptCustomStyles?: boolean;
    ' h5 \7 r0 L" I  N; a
  8.   /**
    0 K2 B3 h- K, U
  9.    * The maximum supported Mini Apps version.0 Y2 Z5 b9 K* I( {" y
  10.    * @default Extracted using the `retrieveLaunchParams`( t) ^$ k, E) x0 `" I! R: Z
  11.    * function.8 N9 u9 V: o% O' o
  12.    * @see retrieveLaunchParams
    8 j! U# m: h/ K" O! r  O
  13.    */
    " Z1 W$ u5 x0 L/ s2 d7 V: v
  14.   version?: Version;
    . i2 m' ~4 y6 y* M, C# u% \
  15.   /**
      ^. r; d. ~5 B
  16.    * Custom postEvent function.. H% W. V9 M& x( _$ R/ [, ]
  17.    *
    2 R% A7 [& y! K/ E- e: Z- p
  18.    * Passing the "strict" value creates a function that always
    " O3 x1 `4 }/ Z
  19.    * checks if the specified call is supported by the current Mini
    2 Q1 E* m, b9 a+ e" ]1 O9 b
  20.    * Apps version. If the method is unsupported, an error
    7 \* V8 }0 T% V% @+ h
  21.    * will be thrown.' t/ d/ e* {9 r9 r  w4 v( N
  22.    *9 M9 [8 x) r9 V4 o5 [; z
  23.    * Passing the "non-strict" value creates a postEvent function9 a/ @, ^* N; {) S6 W  E
  24.    * that doesn't throw errors but warns about missing method
    ; z, Z* }5 V2 E$ q' c9 L+ Y
  25.    * support.& c% E* X' N& I$ b
  26.    *
    # M& e5 X6 ~9 W2 W- D# W
  27.    * @default 'strict'
    & _7 k. ?+ O% z! L1 H. S% p2 N
  28.    * @see createPostEvent* k$ P* ?6 b# O, t# l! B) T& k9 M
  29.    */9 L* O3 E1 @% p: \8 p. {: D1 V3 K
  30.   postEvent?: PostEventFn | 'strict' | 'non-strict';
    9 J' F7 N8 K! b$ z( h5 o& }0 Z) Q
  31. }
复制代码

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

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

  1. import { init } from '@telegram-apps/sdk';
    7 s  ]2 H/ N- S! C! k5 Z" ^
  2. % x$ Y  V; U6 b; ?9 G
  3. init();
复制代码

/ u% N  p) W: i1 _
9 _1 ^/ S; s* g& h3 J/ b/ T8 h
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则