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