填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

TransferState

从服务器端的应用程序传到客户端的应用程序的键值存储。

A key value store that is transferred from the application on the server side to the application on the client side.

查看"说明"...

      
      class TransferState {
  get<T>(key: StateKey<T>, defaultValue: T): T
  set<T>(key: StateKey<T>, value: T): void
  remove<T>(key: StateKey<T>): void
  hasKey<T>(key: StateKey<T>)
  onSerialize<T>(key: StateKey<T>, callback: () => T): void
  toJson(): string
}
    

Provided in

说明

TransferState 将作为可注入令牌提供。要使用它,请在服务器上导入 ServerTransferStateModule,并在客户端上导入 BrowserTransferStateModule

TransferState will be available as an injectable token. To use it import ServerTransferStateModule on the server and BrowserTransferStateModule on the client.

这里会使用 JSON.stringify/JSON.parse 对存储中的值进行序列化/反序列化。因此,仅布尔、数字、字符串、null 和非类对象能以无损的方式进行序列化和反序列化。

The values in the store are serialized/deserialized using JSON.stringify/JSON.parse. So only boolean, number, string, null and non-class objects will be serialized and deserialized in a non-lossy manner.

方法

获取与键名对应的值。如果找不到键名,则返回 defaultValue

Get the value corresponding to a key. Return defaultValue if key is not found.

      
      get<T>(key: StateKey<T>, defaultValue: T): T
    
参数
key StateKey
defaultValue T
返回值

T

设置与键名对应的值。

Set the value corresponding to a key.

      
      set<T>(key: StateKey<T>, value: T): void
    
参数
key StateKey
value T
返回值

void

从商店中取出键名。

Remove a key from the store.

      
      remove<T>(key: StateKey<T>): void
    
参数
key StateKey
返回值

void

测试存储中是否存在键名。

Test whether a key exists in the store.

      
      hasKey<T>(key: StateKey<T>)
    
参数
key StateKey

注册一个回调,以在调用 toJson 时为指定的键名提供一个值。

Register a callback to provide the value for a key when toJson is called.

      
      onSerialize<T>(key: StateKey<T>, callback: () => T): void
    
参数
key StateKey
callback () => T
返回值

void

将存储的当前状态序列化为 JSON。

Serialize the current state of the store to JSON.

      
      toJson(): string
    
参数

没有参数。

返回值

string