1
我正在尝试实现服务存储库。我知道我可以使用承诺,但我不知道如何实际执行它。这里是我的代码:将存储数据映射到对象
export class Account {
constructor(
public id: String,
public name: String,
)
{ }
}
@Injectable()
export class AccountService {
constructor(private storage:Storage){}
getAccount(id:any): Account {
var account : Account;
this.storage.get("my-db").then((data) => {
if(data && data[id]){
account = new Account(data[id].id,data[id].name);
}
else
account = new Account("","");
});
return account;
}
}
,当我使用它:
...
constructor(public accountService:AccountService) {
console.log(accountService.getAccount(1111));
}
...
返回undefined
。
使其工作的最佳实践是什么?
哇!那很简单。谢谢。 – INgeek