Retrieve data from the localStorage to a list

0

Good morning, I'm developing an app with ionic 3 and I have the following situation:

I am writing a list of products in the localStorage, after that, I need to bring the recorded data to a list of products, that is, each vector item, creates a column in the list. Code:

Write to LocalStorage:

  localStorage.setItem("vetor", JSON.stringify(this.vetCarrinho));

Recover from LocalStorage:

ionViewDidLoad() {

if (this.pedido.key) {
  let prod: any = localStorage.getItem('vetor').split(",");
  if (prod != null) {
    prod = JSON.parse(prod);
    if (this.vetCarrinho.indexOf(prod) === -1) {
        this.vetCarrinho.push(this.pedido.produto);
    }
  }
}

Html:

  <ion-list>
        <ion-item *ngFor="let element of vetCarrinho"> 
          {{element}}    
          <button ion-button color="danger" (click)="deleteProduto(element)" item-end>
            <ion-icon name="trash"></ion-icon>
          </button>      
        </ion-item>     
       </ion-list>  

From this, it plays ALL the elements in JUST ONE COLUMN:

How do I create a column for each element?

    
asked by anonymous 29.11.2018 / 13:37

1 answer

0

Make

  const prod = localStorage.getItem('vetor');
  if (prod != null) {
      this.vetCarrinho = JSON.parse(prod);
  }

Is not enough? :)

Attention that whenever you do

localStorage.setItem("vetor", JSON.stringify(this.vetCarrinho));

You are writing over any data that is saved with the 'vector' key

    
03.12.2018 / 20:39