0
我目前正在完成一个项目,但我有一个小问题。HTTP.get请求在从localstorage获取值时不返回元素
我在本地存储一个值(它正在工作)。 我想在我的提供程序中检索这个值,我可以检索它,但是我不能在this.http.get函数中使用它。
我希望能够用存储的值调用this.http.get。
我的赛道,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);
});
});
}
}
,其中数据是空返回此对象:
现在我的赛道服务。 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);
});
});
}
}
返回此对象,其中数据包含的东西:
因为像2也许3小时我在寻找,我没有找到怎么办......
谢谢你的回答:)
我爱你,谢谢!当我尝试这个解决方案时,我没有尝试使用this.identifiant,只有“识别”,并且它不起作用 – Unyxos
@Unyxos Ahha,很高兴它有帮助。 :-) – Pengyy