Reputation: 1211
I am new to Angular, I was trying to use localStorage API in my app.Whenever I run the application it throws an error TypeError: localStorage.getItems is not a function
in console. Then after I downloaded angular-local-storage
by help of npm.I also imported LocalStorageModule
in app.module.ts
and added it to imports array.But,Now It throws this error:
angular-localstorage.js:24 Uncaught TypeError: Cannot read property 'module' of undefined
at TEST (angular-localstorage.js:24)
at eval (angular-localstorage.js:198)
at Object.../../../../angular-localstorage/angular-localstorage.js (vendor.bundle.js:206)
at __webpack_require__ (inline.bundle.js:55)
at eval (index.js:1)
at Object.../../../../angular-localstorage/index.js (vendor.bundle.js:213)
at __webpack_require__ (inline.bundle.js:55)
at eval (app.module.ts:7)
at Object.../../../../../src/app/app.module.ts (main.bundle.js:36)
at __webpack_require__ (inline.bundle.js:55)<br/>
Code containig getItem function:
export class Init{
load(){
if(localStorage.getItem('markers')===null || localStorage.getItem('markers')===undefined)
{
console.log('No markers found...creating...');
var markers=[
{
name:'Company One',
lat:42.825588,
lng:-71.018029,
draggable:true
},
{
name:'Company Two',
lat:42.868170,
lng:-70.889071,
draggable:false
},
{
name:'Company Three',
lat:42.858279,
lng:-71.930498,
draggable:true
}
];
localStorage.setItem('markers',JSON.stringify(markers));
}
else
{
console.log('Loading markers...')
}
}
}
Code in app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import {MarkerService} from './services/marker.service';
import {LocalStorageModule} from 'angular-localstorage';
import { AgmCoreModule } from '@agm/core';
@NgModule({
imports: [
BrowserModule,
CommonModule,
FormsModule,
LocalStorageModule,
AgmCoreModule.forRoot({
apiKey:''
})
],
providers: [MarkerService],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
Can anyone help me out with this.
Thanks in Advance.
Upvotes: 1
Views: 3694
Reputation: 593
localstorage
API is inbuilt into the Angular framework you won't need an external library for that. You can use it by localStorage.<yourMethodName>
directly into your code
Also, there isn't any method called getItems
.
The list of available methods are...
getItem
removeItem
setItem
clear
Upvotes: 2
Reputation: 10944
The standard localStorage API should work alright, no need to install module for that, use the usual way to get and set items in localStorage:
localStorage.setItem('name', 'ABC');
localStorage.getItem('name');
Upvotes: 1