@acpaas-ui/ngx-localstorage 中文文档教程

发布于 3 年前 浏览 19 更新于 2 年前

@acpaas-ui/ngx-localstorage

antwerp-ui localstorage 服务允许您轻松地在浏览器存储中存储和检索数据。

Usage

import { LocalstorageModule } from '@acpaas-ui/ngx-localstorage';

Documentation

访问我们的文档网站,获取完整的操作文档和指南

Localstorage service

Methods

MethodDescription
setItem(key: string, value: any): voidstore an item in the localstorage and update the subscribers (all data is stringified to a JSON string).
getItem<T = any>(key: string): Tretrieve an item from the localstorage (all data is parsed from a JSON string).
removeItem(key: string): voidremove an item from the localstorage and update the subscribers.
clear(...args): voidclear the localstorage.
select<T = any>(selector: Selector, comparator: Comparator = LocalstorageHelper.comparator): BehaviorSubject<T>get a BehaviorSubject containing the selected value.
clearSubscribers(): voidunsubscribe all subscribers.

Storage type

您可以在 forRoot 导入 LocalstorageModule 时的方法。 默认情况下,该服务将验证类型是否存在并回退到 localStorage。 如果 localStorage 不可用,将使用内存中的 polyfill。

Identifier

您可以提供将在实例化 LocalstorageService 时检查的自定义标识符。 如果在存储中找到的标识符与配置不同,存储将被清除。

这样,您可以使您的应用程序存储失效以防止数据冲突。

Example

import { LocalstorageModule } from '@acpaas-ui/ngx-localstorage';

@NgModule({
    imports: [
        LocalstorageModule.forRoot({
            storageType: 'sessionStorage',
            identifier: 'my-app-v1',
        })
    ]
});

export class AppModule {};
import { LocalstorageService } from '@acpaas-ui/ngx-localstorage';

public user: any;
public item: any;
public timesUsed: any;

constructor(
    private localstorageService: LocalstorageService
) {
    this.user = this.localstorageService.select('user');
    this.timesUsed = 0;
    this.localstorageService.setItem('number', this.timesUsed);
}

public loggedIn(): void {
    this.localstorageService.setItem('user', 'You are logged in');
}

public loggedOut(): void {
    this.localstorageService.setItem('user', 'You are logged out');
}

public init(): void {
    this.localstorageService.removeItem('user');
    this.timesUsed = this.timesUsed + 1;
    this.localstorageService.setItem('number', this.timesUsed);
}

public clear(): void {
    this.localstorageService.clear('user', 'number');
}

public getItem(): any {
    this.item = this.localstorageService.getItem('user');
    this.timesUsed = this.localstorageService.getItem('number');
}
<div class="u-margin-bottom">
    <button type="button" (click)="loggedIn()" class="a-button u-margin-right">
        Log in
    </button>
    <button type="button" (click)="loggedOut()" class="a-button u-margin-right">
        Log out
    </button>
    <button type="button" (click)="init()" class="a-button u-margin-right">
        Count clicks
    </button>
</div>
<div class="u-margin-bottom">
    <button type="button" (click)="getItem()" class="a-button u-margin-right">
        Get from local storage
    </button>
    <button type="button" (click)="clear()" class="a-button">
        Clear local storage
    </button>
</div>
<div class="u-margin-bottom">
    <label class="a-input__label a-input__label--inline">{{ this.item }}</label>
</div>
<div class="u-margin-bottom">
    <label class="a-input__label a-input__label--inline">You clicked the count clicks button this many times: {{ this.timesUsed }}</label>
</div>

Contributing

请访问我们的贡献指南,了解有关如何贡献的更多信息。

@acpaas-ui/ngx-localstorage

The antwerp-ui localstorage service allows you to easily store and retrieve data in and from your browsers storage.

Usage

import { LocalstorageModule } from '@acpaas-ui/ngx-localstorage';

Documentation

Visit our documentation site for full how-to docs and guidelines

Localstorage service

Methods

MethodDescription
setItem(key: string, value: any): voidstore an item in the localstorage and update the subscribers (all data is stringified to a JSON string).
getItem<T = any>(key: string): Tretrieve an item from the localstorage (all data is parsed from a JSON string).
removeItem(key: string): voidremove an item from the localstorage and update the subscribers.
clear(...args): voidclear the localstorage.
select<T = any>(selector: Selector, comparator: Comparator = LocalstorageHelper.comparator): BehaviorSubject<T>get a BehaviorSubject containing the selected value.
clearSubscribers(): voidunsubscribe all subscribers.

Storage type

You can set the preferred storage type in the forRoot method when importing the LocalstorageModule. The service will verify the type exists and fall back to localStorage by default. If localStorage is not available, an in-memory polyfill will be used.

Identifier

You can provide a custom identifier that will be checked on instantiating the LocalstorageService. If the identifier found in the storage is different from the config, the storage will be cleared.

This way, you can invalidate your apps storage to prevent data conflicts.

Example

import { LocalstorageModule } from '@acpaas-ui/ngx-localstorage';

@NgModule({
    imports: [
        LocalstorageModule.forRoot({
            storageType: 'sessionStorage',
            identifier: 'my-app-v1',
        })
    ]
});

export class AppModule {};
import { LocalstorageService } from '@acpaas-ui/ngx-localstorage';

public user: any;
public item: any;
public timesUsed: any;

constructor(
    private localstorageService: LocalstorageService
) {
    this.user = this.localstorageService.select('user');
    this.timesUsed = 0;
    this.localstorageService.setItem('number', this.timesUsed);
}

public loggedIn(): void {
    this.localstorageService.setItem('user', 'You are logged in');
}

public loggedOut(): void {
    this.localstorageService.setItem('user', 'You are logged out');
}

public init(): void {
    this.localstorageService.removeItem('user');
    this.timesUsed = this.timesUsed + 1;
    this.localstorageService.setItem('number', this.timesUsed);
}

public clear(): void {
    this.localstorageService.clear('user', 'number');
}

public getItem(): any {
    this.item = this.localstorageService.getItem('user');
    this.timesUsed = this.localstorageService.getItem('number');
}
<div class="u-margin-bottom">
    <button type="button" (click)="loggedIn()" class="a-button u-margin-right">
        Log in
    </button>
    <button type="button" (click)="loggedOut()" class="a-button u-margin-right">
        Log out
    </button>
    <button type="button" (click)="init()" class="a-button u-margin-right">
        Count clicks
    </button>
</div>
<div class="u-margin-bottom">
    <button type="button" (click)="getItem()" class="a-button u-margin-right">
        Get from local storage
    </button>
    <button type="button" (click)="clear()" class="a-button">
        Clear local storage
    </button>
</div>
<div class="u-margin-bottom">
    <label class="a-input__label a-input__label--inline">{{ this.item }}</label>
</div>
<div class="u-margin-bottom">
    <label class="a-input__label a-input__label--inline">You clicked the count clicks button this many times: {{ this.timesUsed }}</label>
</div>

Contributing

Visit our Contribution Guidelines for more information on how to contribute.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文