gpt4 book ai didi

Angular2 - 在哪里可以使用 InMemoryWebApiModule 配置端点?

转载 作者:可可西里 更新时间:2023-11-01 17:25:56 26 4
gpt4 key购买 nike

我正在关注 this tutorial (Angular2 官方“快速入门”教程)

最后,有几个 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.

  1. url - the request URL string
  2. 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.

原来如此!

关于Angular2 - 在哪里可以使用 InMemoryWebApiModule 配置端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47435062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com