Home > @dinofe/xt-core > getStorageSize

# getStorageSize() function

获取 Stroage (opens new window) 存储占用空间的大小

Signature:

export declare function getStorageSize(type: StorageType): IStorageSize;

# Parameters

Parameter Type Description
type StorageType 存储类型

Returns:

IStorageSize

Stroage 存储占用空间大小

# Exceptions

TypeError (opens new window) 当传入的参数 type 不是 StorageType.localStorage | StorageType.sessionStorage 之一时会报错:the param type should be one of ["localStorage", "sessionStorage"]

Error (opens new window)Storage 不可用时会报错:window.${type} is not available

使用时,最好先检查 Storage 是否可用,可以使用方法:validateStorageAvailable(type)

# Remarks

同时支持 localStroage (opens new window)sessionStorage (opens new window)

# Example 1

获取 localStorage 占用空间大小

import { getStorageSize } from '@dinofe/xt-core/web'
const localSize = getStorageSize('localStorage')

# Example 2

获取 sessionStorage 占用空间大小

import { getStorageSize } from '@dinofe/xt-core/web'
const sessionSize = getStorageSize('sessionStorage')

# Example 3

枚举参数可以引入

import { StorageType, getStorageSize } from '@dinofe/xt-core/web'
const localSize = getStorageSize(StorageType.localStorage)