Angular2 - 在哪里可以使用 InMemoryWebApiModule 配置端点?
Angular2 - Where can be endpoints configured using InMemoryWebApiModule?
我正在关注 this tutorial(Angular2 官方 "Quick Start" 教程)
最后,有几个 http 请求(GET、POST、PUT、DELETE...),它们不是调用真实服务器,而是使用以下行模拟的app.module.ts
:
// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
InMemoryDataService, { dataEncapsulation: false }
)
这是应用程序发出的 HTTP 请求的示例:
private heroesUrl = 'api/heroes'; // URL to web api
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}
问题:有什么方法可以配置此 InMemoryDataService 所需的端点吗?因此,例如,我可以将 Url 更改为 api/heroes/getAll
.
根据@Szarik 的建议,我关注 this link,其中有答案。实际上,这是我使用的库的官方规范:in-memory-web-api
在 README.md 中稍微下潜,我们到达:
You can override the default parser by implementing a parseRequestUrl method in your InMemoryDbService.
The service calls your method with two arguments.
- url - the request URL string
- requestInfoUtils - utility methods in a RequestInfoUtilities object, including the default parser. Note that some values have not yet been set as they depend on the outcome of parsing.
Your method must either return a ParsedRequestUrl object or null|undefined, in which case the service uses the default parser. In this way you can intercept and parse some URLs and leave the others to the default parser.
就是这样!
为了举例说明上面的答案:
'forRoot' 采用可选设置,其中有一个 'apiBase'。匹配
的端点
private url = '/some/endpoint/responseExample';
设置对应的'apiBase'无前缀和后缀斜线:
HttpClientInMemoryWebApiModule.forRoot(InMemoryDbItemService, { apiBase: 'some/endpoint'})
'responseExample'为实际数据
createDb() {
const responseExample = {[some:'structure']}
return { responseExample };
}
我正在关注 this tutorial(Angular2 官方 "Quick Start" 教程)
最后,有几个 http 请求(GET、POST、PUT、DELETE...),它们不是调用真实服务器,而是使用以下行模拟的app.module.ts
:
// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
InMemoryDataService, { dataEncapsulation: false }
)
这是应用程序发出的 HTTP 请求的示例:
private heroesUrl = 'api/heroes'; // URL to web api
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}
问题:有什么方法可以配置此 InMemoryDataService 所需的端点吗?因此,例如,我可以将 Url 更改为 api/heroes/getAll
.
根据@Szarik 的建议,我关注 this link,其中有答案。实际上,这是我使用的库的官方规范:in-memory-web-api
在 README.md 中稍微下潜,我们到达:
You can override the default parser by implementing a parseRequestUrl method in your InMemoryDbService.
The service calls your method with two arguments.
- url - the request URL string
- requestInfoUtils - utility methods in a RequestInfoUtilities object, including the default parser. Note that some values have not yet been set as they depend on the outcome of parsing.
Your method must either return a ParsedRequestUrl object or null|undefined, in which case the service uses the default parser. In this way you can intercept and parse some URLs and leave the others to the default parser.
就是这样!
为了举例说明上面的答案: 'forRoot' 采用可选设置,其中有一个 'apiBase'。匹配
的端点private url = '/some/endpoint/responseExample';
设置对应的'apiBase'无前缀和后缀斜线:
HttpClientInMemoryWebApiModule.forRoot(InMemoryDbItemService, { apiBase: 'some/endpoint'})
'responseExample'为实际数据
createDb() {
const responseExample = {[some:'structure']}
return { responseExample };
}