2017-04-14 51 views
0

我知道这可能不是100%正确的方法,因此建议进行清理也很受欢迎。如何返回来自ionic2/3存储的列表/值

刚刚学习/习惯了Ionic2,我无法返回存储项目。

服务:

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

@Injectable() 
export class LocationItemService { 
    locationItems: LocationItem[] = []; 

    constructor(
    private storage: Storage 
) {} 

    getListItems(id): Promise<LocationItem[]> { 
    this.storage.get("Location_Items_" + id).then((val) => { 
     // This does console out the proper value. 
     console.log(val); 
     // Thus Promise contains the proper value. 
     return Promise.resolve(val); 
    }); 
    } 
} 

然而,当我在开发运行我的申请,我得到这个错误: “的函数声明类型既不是‘无效’,也没有‘任意’必须返回值“。

即使我设置this.locationItems,我仍然需要返回承诺,以便下一个函数不会运行,直到Promise被解析。

我觉得它必须处理storage.get内部的范围,而不是直接在函数本身内部,并尝试了几个解决方法无济于事。感谢您提供任何意见!

回答

0

Oi,我没有在this.storage之前使用return。因为它已经被定义为一个承诺。因此,我的回报声明基本陷于僵局。