所以目前我正在使用 Angular 编写一个前端应用程序(不是很重要),并且我有一个当前提供硬编码 json 数据的服务。
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));
}
}
目前可观察并没有多大意义,在真正将所有这些连接到后端之前,我想从不同的文件加载所有数据并模拟 ajax 调用或 http 请求,这将在稍后实现。
有没有办法模拟这种行为并从单独的文件中获取数据?
最佳答案
看看这个 https://github.com/service-mocker/service-mocker以及其他此类模拟 API 生成器。
关于javascript - 模拟http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091065/