2 Home
Ghost edited this page 2023-12-20 05:33:46 +00:00

persist

persist

Table of contents

Classes

Decorators

Options

Decorators

persist

persist<T>(options?): (target: any, prop: any) => void

Sync class property with persistent storage (LocalStorage by default)

Type parameters

Name
T

Parameters

Name Type Description
options? PersistOptions<T> & { key?: string } Configure using PersistOptions

Returns

fn

Decorator function

▸ (target, prop): void

Parameters
Name Type
target any
prop any
Returns

void

Example

class ThemeEngine {
  @persist({default: 'os'}) current!: string;
}

const theme = new ThemeEngine();
console.log(theme.current) // Output: os

theme.current = 'light'; //Any changes will be automatically saved to localStorage

location.reload(); // Simulate refresh
console.log(theme.current) // Output: light

Defined in

persist.ts:135

Options

PersistOptions

Ƭ PersistOptions<T>: Object

Configurations persistence behaviour

Type parameters

Name
T

Type declaration

Name Type Description
default? T Default/Initial value if undefined
storage? Storage Storage implementation, defaults to LocalStorage
type? any Force value to have prototype

Defined in

persist.ts:6