IMO,更好的选择是使用angular-in-memory-web-api。
注意:这个项目是从its old location拉到angular/angular的。
它模拟Http 使用的后端,因此它不会进行实际的 XHR 调用,而是获取您提供给它的数据。要获得它,只需安装
npm install --save angular-in-memory-web-api
要创建数据库,请在 InMemoryDbService 中实现 createDb 方法
import { InMemoryDbService } from 'angular-in-memory-web-api'
export class MockData implements InMemoryDbService {
let cats = [
{ id: 1, name: 'Fluffy' },
{ id: 2, name: 'Snowball' },
{ id: 3, name: 'Heithcliff' },
];
let dogs = [
{ id: 1, name: 'Clifford' },
{ id: 2, name: 'Beethoven' },
{ id: 3, name: 'Scooby' },
];
return { cats, dogs, birds };
}
然后配置一下
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
@NgModule({
imports: [
HttpModule,
InMemoryWebApiModule.forRoot(MockData, {
passThruUnknownUrl: true
}),
]
})
现在,当您使用Http 并向/api/cats 发出请求时,它将从数据库中获取所有猫。如果你去/api/cats/1,它会得到第一只猫。您可以执行所有 CRUD 操作,GET、POST、PUT、DELETE。
需要注意的一点是它需要一个基本路径。在示例中,/api 是基本路径。您还可以在配置中配置根(这与基本不同)路径
InMemoryWebApiModule.forRoot(MockData, {
rootPath: 'root',
passThruUnknownUrl: true // forwards request not in the db
})
现在您可以使用/root/api/cats。
更新
关于如何从开发切换到生产的问题,您可以使用工厂来创建提供程序。如果您要使用模拟服务而不是 in-memory-web-api,情况也是如此
providers: [
Any,
Dependencies
{
// Just inject `HeroService` everywhere, and depending
// on the environment, the correct on will be chosen
provide: HeroService,
useFactory: (any: Any, dependencies: Dependencies) => {
if (environment.production) {
return new HeroService(any, dependencies);
} else {
return new MockHeroService(any, dependencies);
}
},
deps: [ Any, Dependencies ]
]
至于 in-memory-web-api,我需要回复您(我需要测试一个理论)。我刚开始使用它,还没有到需要切换到生产的地步。现在我只有上面的配置。但我确信有一种方法可以让它工作而无需更改任何内容
更新 2
好的,对于 im-memory-web-api,我们可以做的不是导入模块,而是只提供模块提供的XHRBackend。 XHRBackend 是 Http 用来进行 XHR 调用的服务。 in-memory-wep-api 模拟该服务。这就是模块所做的一切。所以我们可以自己提供服务,使用工厂
@NgModule({
imports: [ HttpModule ],
providers: [
{
provide: XHRBackend,
useFactory: (injector: Injector, browser: BrowserXhr,
xsrf: XSRFStrategy, options: ResponseOptions): any => {
if (environment.production) {
return new XHRBackend(browser, options, xsrf);
} else {
return new InMemoryBackendService(injector, new MockData(), {
// This is the configuration options
});
}
},
deps: [ Injector, BrowserXhr, XSRFStrategy, ResponseOptions ]
}
]
})
export class AppHttpModule {
}
注意 BrowserXhr、XSRFStrategy 和 ResponseOptions 依赖项。这就是创建原始XHRBackend 的方式。现在无需将HttpModule 导入您的应用模块,只需导入AppHttpModule。
至于environment,这是您需要弄清楚的事情。有了 angular-cli,当我们在生产模式下构建时,它已经是一个自动切换到生产环境的环境。
这是我用来测试的完整示例
import { NgModule, Injector } from '@angular/core';
import { HttpModule, XHRBackend, BrowserXhr,
ResponseOptions, XSRFStrategy } from '@angular/http';
import { InMemoryBackendService, InMemoryDbService } from 'angular-in-memory-web-api';
let environment = {
production: true
};
export class MockData implements InMemoryDbService {
createDb() {
let cats = [
{ id: 1, name: 'Fluffy' }
];
return { cats };
}
}
@NgModule({
imports: [ HttpModule ],
providers: [
{
provide: XHRBackend,
useFactory: (injector: Injector, browser: BrowserXhr,
xsrf: XSRFStrategy, options: ResponseOptions): any => {
if (environment.production) {
return new XHRBackend(browser, options, xsrf);
} else {
return new InMemoryBackendService(injector, new MockData(), {});
}
},
deps: [ Injector, BrowserXhr, XSRFStrategy, ResponseOptions ]
}
]
})
export class AppHttpModule {
}