I'm facing an issue - how can I store data in localStorage that was received from the server? Should I use localStorage.setItem for this purpose? And how do I handle storing an array in localStorage? Or am I missing something here?
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
export class Contact {
constructor(
public name:string,
public username:string,
public email:string,
public phone:string,
public website:string
) {}
}
@Component({
selector: 'app-contact-list',
templateUrl: './contact-list.component.html',
styleUrls: ['./contact-list.component.css']
})
export class ContactListComponent implements OnInit {
contacts: Contact[] = [];
constructor(
private httpClient: HttpClient,
) { }
ngOnInit(): void {
this.contacts = [];
this.getContacts();
}
getContacts() {
this.httpClient.get<any>('some url').subscribe(
response => {
console.log(response);
this.contacts = response;
}
);
}
}