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

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

回答

收藏

Telegram 小程序 | TypeScript @telegram-apps/sdk @2.x | 组件 | 云存储

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

云存储

负责管理 Telegram 迷你应用程序中云存储的💠组件

检查支持

要检查当前 Telegram 迷你应用程序版本是否支持云存储,需要使用 isSupported 方法:

Variable :

  1. import { cloudStorage } from '@telegram-apps/sdk';
    " f  [2 @1 e7 `$ H: [+ H8 m6 S5 u( ^

  2. : `& s/ R7 x; O
  3. cloudStorage.isSupported(); // boolean
复制代码
+ p% ]- b; H0 }* }: _

Functions :

  1. import { isCloudStorageSupported } from '@telegram-apps/sdk';
    % c' [$ u( d9 F

  2. 5 x/ _4 I: X+ u: H7 h
  3. isCloudStorageSupported(); // boolean
复制代码
设置项目

要设置键值,请使用 setItem 方法。

Variable :

  1. await cloudStorage.setItem('a', 'a-value');
复制代码

Functions :

  1. import { setCloudStorageItem } from '@telegram-apps/sdk';
    0 N8 d5 Y/ y8 O, x3 M7 ?

  2. 6 i' t. ]8 w2 l) b& b6 Q& C, c
  3. await setCloudStorageItem('a', 'a-value');
复制代码
获取钥匙

要获取所有现有密钥的列表,请使用 getKeys 方法。

Variable :

  1. const keys = await cloudStorage.getKeys(); // ['a', 'b', 'c']
复制代码

Functions :

  1. import { getCloudStorageKeys } from '@telegram-apps/sdk';
    6 M3 U. L1 R0 Z* j3 |: R
  2. " e7 l3 X6 j$ T/ `3 B9 U- h
  3. const keys = await getCloudStorageKeys(); // ['a', 'b', 'c']
复制代码

! E3 T0 d2 L& e8 m2 h

要获取特定键或多个键的值,请使用 getItem 方法。

Variable :

  1. const nonExistent = await cloudStorage.getItem('non-existent');
    7 L' k& A7 w; a
  2. // The result is an empty string: ''3 H; F" v2 b; H0 q" h
  3. 1 p8 J2 ~6 y" d( _; x
  4. const existent = await cloudStorage.getItem('a');
    0 e5 J# _* R) G0 ^4 K
  5. // The result is the value of the 'a' key. Example: 'a-value'
    / ^! H- i# S+ |- I

  6. - [8 f+ k( c4 ]- K5 {; @
  7. const values = await cloudStorage.getItem(['a', 'b', 'non-existent']);, y. a' Q4 J) g, c+ P
  8. // The result is a record of the keys 'a', 'b', and 'non-existent'.
    8 I* |$ d5 L% A
  9. // Example:' s2 o4 Y1 \2 Q: s* y* u! b
  10. // { ( i) H; c9 Y* s$ L: ]  i2 E
  11. //   a: 'a-value', / ?" c# n; \" Q: \
  12. //   b: 'b-value',
    2 A  ]9 I) ?# I& V
  13. //   'non-existent': '', $ u; K, F: y0 M! G4 g9 {
  14. // }
复制代码

Functions :

  1. import { getCloudStorageItem } from '@telegram-apps/sdk';9 Z4 x, x' \0 L" N( J" ^

  2. 2 J' c1 K# i' Y. l- o
  3. const nonExistent = await getCloudStorageItem('non-existent');
    0 Z: R( a7 P5 P/ ^9 Q0 _. [$ `
  4. // The result is an empty string: ''" b  @* C0 t( q9 R/ Q- H$ W

  5. & D" ^; o  W8 B
  6. const existent = await getCloudStorageItem('a');
    ) ^9 E1 @7 h8 R3 F5 @3 Y
  7. // The result is the value of the 'a' key. Example: 'a-value'- u) M! F5 c; z  F) g. @

  8. 3 N. h) H; S: G" ~. c8 X1 D
  9. const values = await getCloudStorageItem(['a', 'b', 'non-existent']);/ L* N5 H, O! ]
  10. // The result is a record of the keys 'a', 'b', and 'non-existent'.
    ) }2 S6 @! t* C5 q; @0 t; g# P6 V
  11. // Example:
    $ v0 D9 b; ~3 I2 ?2 p/ q. M' a% K
  12. // {
    - W. a, L0 P; D, N9 F2 P
  13. //   a: 'a-value',
    ( K8 e$ M# C7 \+ `
  14. //   b: 'b-value',
    4 R; d+ Y9 V' V, O9 w9 h
  15. //   'non-existent': '',
    4 @! H* r' g. o# d4 p2 H/ t* S
  16. // }
复制代码
删除项目

要删除一个键或键列表,请使用 deleteItem 方法。

Variable :

  1. await cloudStorage.deleteItem('a');
    5 ]9 V& Q% s. x3 u
  2. await cloudStorage.deleteItem(['a', 'b', 'c']);
复制代码

Functions :

  1. import { deleteCloudStorageItem } from '@telegram-apps/sdk';+ x2 h* P' o" b! l

  2. % R4 X$ G# k8 \  i. H
  3. await deleteCloudStorageItem('a');- ]5 h. T* f5 x. h9 e1 `
  4. await deleteCloudStorageItem(['a', 'b', 'c']);
复制代码

! ]5 }: c, t% l) o% b0 c* P, n, W3 ~  I% H
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则