我一直在尝试加载本地json数据文件,我将.json文件放在/ src / assets / data中,而我正在使用的提供程序如下:
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));
在本地浏览器中进行测试时,这确实显示了在android上尝试此操作时加载的正确json数据只返回以下错误文本:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/throw';
import { Platform } from 'ionic-angular';
/*
Generated class for the JsonProvider provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class JsonProvider {
private localApiUrl: string;
private remoteApiUrl: string;
constructor(public http: Http, private platform: Platform) {
//console.log('Hello JsonProvider Provider');
if (this.platform.is('cordova') && this.platform.is('android')) {
this.localApiUrl = "file:///android_asset/www/data";
}
else {
this.localApiUrl = "assets/data";
}
}
GetLocalData(file: string): Observable<any> {
return this.http.get(`${this.localApiUrl}/${file}.json`)
.map(this.extractData)
.catch(this.handleError);
}
GetRemoteData(url: string): Observable<string[]> {
return this.http.get(`${this.remoteApiUrl}/${url}`)
.map(this.extractData)
.catch(this.handleError);
}
GetGareList(): Observable<any> {
return this.GetLocalData("gareList");
}
GetGara(Id: number): Observable<any> {
return this.http.get(`${this.localApiUrl}/gare.json`)
.map(res => {
let body = res.json();
if (body) {
return body.filter(x => { return x.Id == Id })[0];
}
else {
return {};
}
})
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
我在没有0 - {"isTrusted":true}
部分的情况下也尝试了但仍然无法加载文件。
答案 0 :(得分:0)
你应该试试
React doesn't call componentWillReceiveProps with initial props during mounting. It only calls this method if some of component's props may update. Calling this.setState generally doesn't trigger componentWillReceiveProps.
无需检查您所在的平台。