Angular:How 在控制台中打印完整的 URL

Angular:How to print full URL in console

我正在研究 angular 4,如何在控制台中像 http://somthing/getHeros?hero=hero 一样打印完整的 url。我的服务方式是

getHeros(hero: string): Observable<Hero[]>{
const url = "http/somthing/getHeros",
const params = new HttpParams.set("hero", hero);
console.log(this.httpClient.get<Hero[]>(url, {params: params}));
return this.httpClient.get<Hero[]>(url, {params: params})

我上面的控制台不打印 URL 它打印响应。请帮我打印完整 URL.

如果您只想阅读 URL 进行调试,您可以使用浏览器开发工具的 "Network" 面板。

如果您想在代码中访问 URL,您可以编写一个 HTTP 拦截器(例如,参见 http-interceptor)。拦截器接收一个 HttpRequest 对象,其中包含 urlparamsurlWithParams 属性。

如您所说,hero 是字符串,您可以像 const url = "http/somthing/getHeros/"+hero; 一样直接创建 url。 然后将它传递给 get() 方法,它会 return 你回应。

getHeros(hero: string): Observable<Hero[]>{
    const url = "http/somthing/getHeros?"+hero;
    console.log(url);
    return this.httpClient.get<Hero[]>(url);

打开 Chrome 检查器。单击网络选项卡。点击 XHR。如果您发出 XHR 请求,它将与整个请求 headers、正文等一起显示在网络选项卡上。