weixin_33690963 2018-06-28 20:52 采纳率: 0%
浏览 32

模拟http请求[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/44042223/load-json-from-local-file-with-http-get-in-angular-2" dir="ltr">Load json from local file with http.get() in angular 2</a>
                            <span class="question-originals-answer-count">
                                (7 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-07-02 11:44:35Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

So currently im programming a front end application with Angular (not really important) and i have a service which currently provides hard coded json data.

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Client } from '../models/client';

@Injectable({
  providedIn: 'root'
})
export class ClientService {

  clientsDataMock : Client[] = [    
    {name: "Client A", id: "15", profiles: [
      {name: "profile1"},
      {name: "profile2"},
      {name: "profile3"}
    ]},
    {name: "Client B", id: "20", profiles: [
      {name: "profileX"}
    ]},
    {name: "Client C", id: "25", profiles: [
      {name: "profileY"}
    ]}
  ];
  constructor() { }

  getClients(): Observable<Client[]> {
    return of(this.clientsDataMock);
  }

  getClient(id : String) : Observable<Client>{
    return of(this.clientsDataMock.find(client => client.id == id));
  }
}

Currently the observable doesnt really make much sense and before really connecting all this to the backend i want to load all the data from a different file and kinda simulate the ajax calls or http request, which would be implemented later.

Is there a way to simulate this behavior and fetch the data from a seperate file?

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33690963 2018-06-28 20:55
    关注

    Have a look through this https://github.com/service-mocker/service-mocker and other such mock API generators.

    评论

报告相同问题?