[英]How to mock an http.get in angular2
讓我解釋。
我在“ matiere.service.ts”中有這種要求
import { Injectable } from '@angular/core';
import {Http, RequestMethod} from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class MatiereService {
private baseUrl = 'http://localhost:8080/tpFormation-web';
constructor(private http: Http) { }
list() {
return this.http.get(`${this.baseUrl}/matiere`);
}
find(id) {
return this.http.get(`${this.baseUrl}/matiere/${id}`);
}
}
我想創建一個fakematiere.service.ts之類的虛假請求。 我會照做的,因為我要連接的REST api尚未編碼。 我已經檢查了“ in-memory-web-api”,但是我不知道它是否是正確的工具,但是,我真的不知道如何實現它。
有什么幫助嗎?
按照@Rahul所說的,如果您只是做gets,那么json文件又快又容易。
我這里有一個使用json文件的示例: https : //github.com/DeborahK/Angular2-GettingStarted
基本上,您只需要更改以下代碼行:
private baseUrl = 'http://localhost:8080/tpFormation-web';
指向您的json文件。
但是,如果您有put / post / etc,那么在內存中使用api是可行的方法。
我在這里有一個在內存中使用API的示例: https : //github.com/DeborahK/Angular-Routing
我用過
npm install -g json-server
完美地工作!
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.