Ngx_webstorage检索值为空



我正在使用带有ngx-webstorage的角度 2 .

首先我安装npm install --save ngx-webstorage.

这是我的应用程序.模块.ts

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {Ng2Webstorage} from 'ngx-webstorage';
@NgModule({
declarations: [...],
imports: [
BrowserModule,
Ng2Webstorage,
],
bootstrap: [...]
})
export class AppModule {
}

这是我的另一个组件。

import {Component} from '@angular/core';
import {LocalStorageService} from 'ngx-webstorage';
@Component({
selector: 'foo',
template: `
<section>{{attribute}}</section>
<section><button (click)="add()">Add</button></section>
<section><button (click)="retrieveValue()">Retrieve</button></section>
`,
})
export class FooComponent {
attribute;
constructor(private storage:LocalStorageService) {}
add(){
this.storage.store('boundValue',"Test");
}
retrieveValue() {
this.attribute = this.storage.retrieve('boundValue');
console.log(this.attribute);
}
}

当我单击Add按钮时,我存储boundvalue,然后单击Retrieve按钮检索'boundvalue'

这是工作。但是我刷新了我的网页。boundvalue为空。

我每次都需要边界值。我该怎么做?

当我遇到类似的问题时,那是因为我无意中清除了某个地方的存储。在你的项目中搜索"this.storage.clear",或者查看你导入ngx-webstorage的每个组件,看看你是否在任何地方清除了你的存储。

最新更新