Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use angular2-local-storage module in Angular 2 app

Tags:

angular

How do I use this module in Angular 2? Any examples?

So far I installed the module and imported in the Angular 2 component.

https://www.npmjs.com/package/angular2-local-storage

like image 640
slowydown Avatar asked Feb 10 '16 16:02

slowydown


2 Answers

Import it and add it to bootstrap()

bootstrap(AppComponent, [OtherProvider, LocalStorage]);

Inject it into the component, directive, or service where you want to use it.

export class SomeComponent {
  constructor(private ls:LocalStorage) {}
  clickHandler() {
    this.ls.set('someKey', 'someValue');
  }
  otherClickHandler() {
    console.log(this.ls.get('someKey'); // should print 'someValue'
  }
}
like image 183
Günter Zöchbauer Avatar answered Sep 23 '22 22:09

Günter Zöchbauer


I have faced same issue and after debugging code I found the solution.

Issue is with npm package creation not in the source code.

When you run "npm install angular2-localStorage", it download package in node_module folder but in web deployment, that folder goes outside of node_module. Because of this, website can not find the package hence the error you get in browser.

I have copied those 2 files "LocalStorage.ts" and "LocalStorageEmitter.ts" and referred directly in my code.

It works like a charm!

like image 28
Balran Chavan Avatar answered Sep 25 '22 22:09

Balran Chavan