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

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

回答

收藏

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

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

初始化

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

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

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

  1. interface Options {
    / L) f/ q9 W, z) C' ^7 B
  2.   /**
    6 q4 D7 X3 t3 t9 S& i& v- p
  3.    * True if the SDK should accept styles sent from the Telegram
    ( g; J1 \- H' @1 B, c  l1 p
  4.    * application.5 A5 C% N& W4 {( P6 M- x" z1 [  Q
  5.    * @default true1 H% ^+ E9 X8 C# |( q; ^& q
  6.    */
    1 ^+ l6 d, Q( s2 v1 q, B
  7.   acceptCustomStyles?: boolean;
    3 Y2 Z1 {* P6 d5 z7 k/ `
  8.   /**
    2 p; \6 s" t( I6 T! L; C# t& s
  9.    * The maximum supported Mini Apps version.
    * Q0 F# V3 F1 N- @& [, u6 ?
  10.    * @default Extracted using the `retrieveLaunchParams`
    ( b* K5 h+ \0 i; t& h0 x
  11.    * function.
    ' `( y* k  Y+ A9 @8 _% I
  12.    * @see retrieveLaunchParams/ @) I4 n2 [9 L9 N$ d: E$ ^
  13.    */
    0 w  }- M7 n7 P% i8 [0 g
  14.   version?: Version;7 X, [6 h( e) s+ G3 u, r, F3 J( K
  15.   /**3 r- H/ h* z( ]* \. _7 Y, q
  16.    * Custom postEvent function.
    7 ^! M: D  C5 h; V
  17.    *
    ! _0 N5 n+ y( ]" h" R7 p
  18.    * Passing the "strict" value creates a function that always
    9 P3 p6 C3 P* s1 A6 B
  19.    * checks if the specified call is supported by the current Mini6 r- _( u/ `. U) }, q
  20.    * Apps version. If the method is unsupported, an error) a6 l+ C- A0 V" N
  21.    * will be thrown.
    8 K* D$ o/ c/ ]) b6 O7 H
  22.    *1 V" o" f" _* B0 j# x# G* `2 C# ^
  23.    * Passing the "non-strict" value creates a postEvent function
    / x8 Z1 V! A, h/ c
  24.    * that doesn't throw errors but warns about missing method
    ; I% l: E, [8 K# x5 b
  25.    * support.
    , L7 i0 B9 p: I! N: }+ x$ d
  26.    *8 I5 ~# e! L8 W/ u" f/ U
  27.    * @default 'strict'. b* h$ j8 G" ~, H
  28.    * @see createPostEvent
    3 N" e: r+ Z" Y
  29.    */! j9 D7 z" x$ a  ?* B5 G. G
  30.   postEvent?: PostEventFn | 'strict' | 'non-strict';/ P) U3 h' G, M, |; a" H( T
  31. }
复制代码

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

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

  1. import { init } from '@telegram-apps/sdk';
    & U( x4 A7 G9 E# W
  2. " [% G# Y' `( ^/ a5 _) v
  3. init();
复制代码
% _, A. h- n; l

9 Z7 U" S" {* E: Z2 V9 v/ L" i
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则