我目前正在完成一个项目,但我有一点问题。
我将值存储在本地(它正在工作)。 我想在我的Provider中检索这个值,我可以检索它,但是我不能在this.http.get函数中使用它。
我希望能够使用存储的值调用this.http.get。
我的cours-services.ts 无效:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Storage} from '@ionic/storage';
import 'rxjs/add/operator/map';
import {resolveId} from "@ionic/app-scripts/dist/rollup/ionic-rollup-resolver-plugin";
/*
Generated class for the CoursService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class CoursService {
data: any;
identifiant: string;
constructor(public http: Http, private storage: Storage) {
console.log('Hello CoursService Provider');
this.storage = storage;
}
getName() {
};
load() {
if (this.data) {
// already loaded data
return Promise.resolve(this.data);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
this.storage.get('identifiant').then((identifiant) => {
this.identifiant = identifiant
})
this.http.get('https://api.corentincloss.fr/intranet/edt.php?id=' + this.identifiant)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
resolve(this.data);
});
});
}
}
返回数据 null :
的数据现在我的cours-service.ts在我手动定义this.identifiant时工作:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Storage} from '@ionic/storage';
import 'rxjs/add/operator/map';
import {resolveId} from "@ionic/app-scripts/dist/rollup/ionic-rollup-resolver-plugin";
/*
Generated class for the CoursService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class CoursService {
data: any;
identifiant: string;
constructor(public http: Http, private storage: Storage) {
console.log('Hello CoursService Provider');
this.storage = storage;
}
getName() {
};
load() {
if (this.data) {
// already loaded data
return Promise.resolve(this.data);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
/*this.storage.get('identifiant').then((identifiant) => {
this.identifiant = identifiant
})*/
this.identifiant = 'closs006'
this.http.get('https://api.corentincloss.fr/intranet/edt.php?id=' + this.identifiant)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
resolve(this.data);
});
});
}
}
返回数据包含内容的对象:
我正在搜索,因为我可能会花费3个小时,而且我找不到怎么办...
感谢您的回答:)
答案 0 :(得分:1)
因为你知道this.storage.get
是一种异步方式,所以你不会立即得到你想要的东西。所以你必须将http.get移动到this.storage.get().then()
。
this.storage.get('identifiant').then((identifiant) => {
this.identifiant = identifiant
this.http.get('https://api.corentincloss.fr/intranet/edt.php?id=' + this.identifiant)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
resolve(this.data);
});
})