2017-12-13 4 views
1

나는 이오니아 앱 개발에 익숙하지 않으며 동일한 지침서를 따라 왔습니다.이온 저장소의 모든 저장 키 가져 오기

나는 IonicStorageModule을 사용하려고 시도했지만 Chrome 브라우저 모바일보기 모드에서 앱을 실행하는 동안 오류가 없더라도 로컬 저장 키가 생성되지 않습니다. 내 app.module.ts에서

, 나는 그런 내가, 내가 user-settings.service.ts라는 이름의 서비스를, imports 배열에 다음과 같은 수입, 이제

imports: [ 
    BrowserModule, 
    HttpModule, 
    IonicModule.forRoot(MyApp), 
    IonicStorageModule.forRoot() 
    ], 

을 만든

import { IonicStorageModule } from '@ionic/storage'; 

import { Injectable } from '@angular/core'; 
import { Storage } from '@ionic/storage'; 

@Injectable() 
export class UserSettings { 
    constructor(private storage:Storage){} 

    favoriteTeam(team,tournamentId, tournamentName) { 
     let item = { team: team,tournamentId : tournamentId, tournamentName : tournamentName}; 
     this.storage.set(team.id , JSON.stringify(item)); 
    } 

    unFavoriteTeam(team) { 
     this.storage.remove(team.id); 
    } 

    getAllFavorites() { 
     let items = []; 
     return this.storage.forEach((v,k,i) => { 
      items.push(JSON.parse(v)); 
     }).then(() => { 
      return items; 
     }); 

    } 
} 

이제 구성 요소 클래스에서 검색하려고합니다. 모든 즐겨 찾기가 있지만 예상대로 작동하지 않습니다.

ionViewDidEnter() { 
    this.userSettings.getAllFavorites().then((val) => { 
     _.forEach(val,(v,k)=> { 
     this.favorites.push(v); 
     }); 
    }); 

    console.log(this.favorites); 
    } 

내가 잘못하고 있나?

이온 저장 장치에서 모든 키 값을 검색하는 방법은 무엇입니까?

올바른 방법입니까? 나에게 많은 반복처럼 보입니다.

당신은 한 번에 모든 좋아하는 팀을 저장할 수

답변

0

, 무엇인가 : 다음

ionViewDidEnter() { 
    this.storage.get('favorites', ...).then(val => { 
    this.favorites = ... 
    } 
} 

을 :

this.storage.set('favorites', teams) 

그래서, ionViewDidEnter에서 당신은 스토리지에서 (목록으로) 모든 즐겨 찾기를로드 할 필요 목록에 항목을 추가하거나 제거하고 저장하십시오.

+0

이미 favTeam 메소드에 fav 팀 세부 정보를 모두 저장하고 있으며 'unFavoriteTeam' 메소드에서 해당하는 정보를 제거하고 있습니다. .forEach를 사용하여 루프를 반복 할 수없는 이유는 무엇입니까? 스토리지에 즐겨 찾기 배열을 다시 설정해야하는 이유는 무엇입니까? – StrugglingCoder