Recover data from localStorage to a list

Asked

Viewed 194 times

0

Good morning, I am developing an app with Ionic 3 and I have the following situation :

I am recording a list of products in the Store, after that, I need to bring the recorded data, for a list of products, IE, each item of the vector, creates a column in the list. Code:

Save to Localstorage:

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

Recovers from the 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 that, he plays ALL elements in ONLY ONE COLUMN :

inserir a descrição da imagem aqui

How do I make it create a column each element ?

  • How’s your vetCarrinho?

  • [cellular,]

  • in my view your code is correct, there is some detail that is not rendering correctly.

  • I tried to use split, to separate by comma, but I was not successful. I edited the question

  • 1

    Try it like this to see: let prod: any = JSON.parse(localStorage.getItem('vetor'));&#xA; if (prod != null) {&#xA; for(let i=0; i<prod.length; i++) {&#xA; if (this.vetCarrinho.indexOf(prod[i]) === -1) {&#xA; this.vetCarrinho.push(this.pedido.produto);&#xA; }&#xA;}&#xA;&#xA; }

  • Dude, before you send to firebase, I’m going through: this.pedido.product = this.vetCart, will that be the problem ?

Show 1 more comment

1 answer

0

Do

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

not enough? :)

Attention that whenever you do

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

You are typing over any data that is stored with the vector key'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.