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
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'
}
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With