Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 多API';内存中的s数据服务在2中不工作_Angular - Fatal编程技术网

Angular 多API';内存中的s数据服务在2中不工作

Angular 多API';内存中的s数据服务在2中不工作,angular,Angular,我正在开发angular 2应用程序,但后端尚未创建。 Angular有一个很好的特性:内存数据服务 我已经让它在1个数据集上工作了,但不适用于多个数据集 在另一个回答中,有人说我可以这样把它们放在一起: 从“角度内存web api”导入{InMemoryDbService}; InMemoryData中的导出类实现InMemoryDbService{ createDb(){ 设类别=[ {id:1,类别:“人”,名称:“testname”,faName:}, {id:2,类别:“货物”,名称

我正在开发angular 2应用程序,但后端尚未创建。 Angular有一个很好的特性:内存数据服务

我已经让它在1个数据集上工作了,但不适用于多个数据集

在另一个回答中,有人说我可以这样把它们放在一起:

从“角度内存web api”导入{InMemoryDbService};
InMemoryData中的导出类实现InMemoryDbService{
createDb(){
设类别=[
{id:1,类别:“人”,名称:“testname”,faName:},
{id:2,类别:“货物”,名称:“testname2”,faName:}
];
let headCategory=[
{id:1,名称:“personen”},
{id:2,姓名:“goederen”}
];
设langText=[
{langId:1,categoryId:1,文本:“testnl”},
{langId:2,categoryId:1,文本:“testfr”},
{langId:1,categoryId:2,文本:“testnl2”},
{langId:2,categoryId:2,文本:“testfr2”},
];
设langs=[
{id:1,langName:“荷兰”,标签:“NL”},
{id:2,langName:“Frans”,tag:“FR”}
];
返回{categories,headCategory,langText,langs};
}

}
我解决了这个问题。问题不在于错误中显示的API。问题是在我调用它时没有[0]。

这是我的InMemoryDbService,我使用了多个API:

import { InMemoryDbService } from 'angular-in-memory-web-api';
import { OrderRequest } from '../../order-window/order-request';
import { Location } from '../location/location';

export class InMemoryDataService implements InMemoryDbService {

createDb() {
let orderRequests: OrderRequest[] = [
  {
    id: 1,
    fullname: 'Jack',
    tel: 4444444,
    watel: 4444444,
    confirmationId: 654645,
    location: 'Bangalore',
    mail: 'jack@gmail.com',
    manual: 'This is testing',
    termsAccepted: true,
    uFile: ''
  }
];

let locations: Location[] = [
  {
      location: 'RT Nagar'
  },
  {
      location: 'Hebbal'
  },
  {
      location: 'Sanjay Nagar'
  },
  {
      location: 'Malleswaram'
  },
  {
      location: 'Sadashivanagar'
  }
];
return {orderRequests, locations};
} }

这是我的位置服务:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';

import { Location } from './location';

@Injectable()
export class LocationService {

private _locationUrl = 'api/locations';

constructor(private _http:Http) {}

getLocations(): Promise<Location[]> {
    return this._http.get(this._locationUrl)
    .toPromise()
    .then(response => response.json().data as Location[])
    .catch(this.handleError);
}

private handleError(error: Response) {
    console.error(error);
    return Promise.reject(error.json().error || 'Server error');
}
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Response};
从“./Location”导入{Location};
@可注射()
出口类定位服务{
私有_locationUrl='api/位置';
构造函数(私有http:http){}
getLocations():承诺{
返回此。\ http.get(此。\位置URL)
.toPromise()
.then(response=>response.json().data作为位置[])
.接住(这个.把手错误);
}
私有句柄错误(错误:响应){
控制台错误(error);
return Promise.reject(error.json().error | |'Server error');
}
}
这是我的OrderRequestService:

import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { OrderRequest } from '../order-window/order-request';

import 'rxjs/add/operator/toPromise';
@Injectable()
export class AdminService {

private headers = new Headers({ 'Content-Type': 'application/json' });
private url = 'api/orderRequests';  // URL to web api

constructor(private http: Http) { }
 getOrderRequests(): Promise<OrderRequest[]> {
  return this.http.get(this.url)
  .toPromise()
  .then(response => response.json().data as OrderRequest[])
  .catch(this.handleError);
 }

 create(orderRequest: OrderRequest) {
  console.log('Create object called. Value is: ' + JSON.stringify(orderRequest));
return this.http
.post(this.url, JSON.stringify(orderRequest), { headers: this.headers})
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);
}

update(orderRequest: OrderRequest): Promise<OrderRequest> {
const url = `${this.url}/${orderRequest.id}`;
return this.http
  .put(url, JSON.stringify(orderRequest), { headers: this.headers })
  .toPromise()
  .then(() => null)
  .catch(this.handleError);
}

delete(id: number): Promise<void> {
const url = `${this.url}/${id}`;
return this.http.delete(url, { headers: this.headers })
  .toPromise()
  .then(() => null)
  .catch(this.handleError);
}

private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Headers,Http,Response};
从“../order window/order request”导入{OrderRequest};
导入“rxjs/add/operator/toPromise”;
@可注射()
导出类管理员服务{
私有头=新头({'Content-Type':'application/json'});
私有url='api/orderRequests';//web api的url
构造函数(私有http:http){}
getOrderRequests():承诺{
返回this.http.get(this.url)
.toPromise()
.then(response=>response.json().data作为OrderRequest[])
.接住(这个.把手错误);
}
创建(orderRequest:orderRequest){
log('Create object called.Value为:'+JSON.stringify(orderRequest));
返回此文件。http
.post(this.url,JSON.stringify(orderRequest),{headers:this.headers})
.toPromise()
.then(res=>res.json().data)
.接住(这个.把手错误);
}
更新(orderRequest:orderRequest):承诺{
常量url=`${this.url}/${orderRequest.id}`;
返回此文件。http
.put(url,JSON.stringify(orderRequest),{headers:this.headers})
.toPromise()
。然后(()=>null)
.接住(这个.把手错误);
}
删除(id:编号):承诺{
常量url=`${this.url}/${id}`;
返回this.http.delete(url,{headers:this.headers})
.toPromise()
。然后(()=>null)
.接住(这个.把手错误);
}
私有句柄错误(错误:任意):承诺{
console.error('发生错误',error);//仅用于演示目的
返回承诺。拒绝(error.message | | error);
}
}

我的服务很好。试着找出你在哪里犯了错误。

你到底是如何使用它的?你是指我访问api的服务代码吗?是的。您还可以添加预期行为和结果吗?在提供预期行为的地方添加服务和代码吗?什么意思?