这可能是由于您的API端点需要斜杠(/)而导致的问题。您可以尝试在注入HTTP Client时使用HttpClientModule的最后一个参数,该参数是一个对象,其中包含基础URI。例如:
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { HttpProxyAgent } from 'https-proxy-agent';
@NgModule({
imports: [
HttpClientModule
]
})
export class MyModule {
constructor(private http: HttpClient) {
const httpProxy = 'http://proxyserver.com:8080';
const agent = new HttpProxyAgent(httpProxy);
const options = {
// include a trailing slash if necessary
baseUrl: 'https://api.example.com/'
};
this.http = new HttpClient({
...options,
// include the agent to proxy requests
httpClientBackend: {
...options,
proxy: {
agent
}
}
});
}
}
在options对象中,您可以将baseURL设置为API的基本URI,并确保它以正斜杠结尾。接下来,在构造函数中,您可以使用HttpClient的最后一个参数来配置http客户端以使用代理。
通过这种方式,您的API路径将始终以正斜杠(/)结尾,而不必手动添加它。