我试图在promise解析后返回一个布尔值,但是typescript会给出错误说法
A 'get' accessor must return a value.
我的代码看起来像.
get tokenValid(): boolean {
// Check if current time is past access token's expiration
this.storage.get('expires_at').then((expiresAt) => {
return Date.now() < expiresAt;
}).catch((err) => { return false });
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于Ionic 3 Application,存储是Ionic Storage实例.
我正在尝试使用离子存储中的令牌从服务器获取数据。我遇到的问题是,获取令牌承诺不能按时检索令牌。因此,每当我重新加载或重新打开该应用程序时,有时都会返回未经授权的错误。
仪表板service.ts
authUrl = "https://sampleapi.herokuapp.com"
authHeaders;
getToken() {
this.storage.get('token').then((token) => {
console.log('Bearer ' + token);
this.authHeaders = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
})
}
});
}
getInfo(): Observable<Info> {
return this.http.get<Info>(this.authUrl + '/api/users/info', this.authHeaders).pipe(
catchError(this.handleError)
);
}
Run Code Online (Sandbox Code Playgroud)
仪表板
ionViewDidLoad() {
this._dashboardService.getToken();
this._dashboardService.getInfo().subscribe(
info => {
console.log('USER INFO: ' + info);
this.info = info
},
error => {
console.log('INFO ERROR: ' + error);
}
);
}
Run Code Online (Sandbox Code Playgroud) 我是ionic 3的新手,我需要在各自的存储中维护脱机数据,并在网络恢复时保留数据。
我进行了很多研究,其中一项是选择天气以使用离子存储,后者在后台使用Cordova sqlite以键值对的形式存储数据或以sqlite的形式编写查询并以表的形式存储数据。处理离线数据时,哪一种效率更高?假设我需要保存一个对象(不超过100个)。我想编写一个查询来选择该对象,或者我需要迭代100次以选择适当的对象?
如果我的问题不清楚,请在下面的评论中提及。
正如标题中所说,我的问题是当我这样做时:
this.storage.get('user')
Run Code Online (Sandbox Code Playgroud)
我得到这个对象
t {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
Run Code Online (Sandbox Code Playgroud)
我不知道如何操纵它。
我在问是否有办法从我的存储示例中获取字符串值
this.storage.get('user')
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用离子存储来存储离子3的Json对象.我发现很难,因为没有离子3的例子而且我被卡住了.网上似乎没有任何更新.Ionic Storage的支持示例将非常有用.先感谢您.