我有一個 Angular Spring Boot 專案。Angular 專案包含向運行在埠 8080 上的 Spring Boot 應用程式發出 Http 請求的服務。當我嘗試在服務環境中使用 apiBaseUrl 變數時,我得到一個 404 狀態,如下所示:

services 正在對 localhost:4200 而不是 localhost:8080 進行 Http 呼叫
環境:
export const environment = {
production: false,
apiBaseUrl: 'http://localhost:8080'
};
服務:
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { environment } from "src/environments/environment";
import { Student } from "./student";
@Injectable ({
providedIn: 'root'
})
export class StudentService {
private apiServerUrl = environment.apiBaseUrl;
constructor(private http: HttpClient) { }
public getStudents(): Observable<Student[]> {
return this.http.get<Student[]>('${this.apiServerUrl}/student');
}
//rest of code
但是當我進行更改并直接提供 url 時,一切正常。
return this.http.get<Student[]>('http://localhost:8080/student');
誰能給我解釋一下,這是我的第一個角度應用程式,所以謝謝你的理解
uj5u.com熱心網友回復:
您應該使用反引號 `,而不是單引號 '
return this.http.get<Student[]>(`${this.apiServerUrl}/student`);
uj5u.com熱心網友回復:
要解決此問題,您可以通過 `` 在此行中更改 '' this.http.get<Student[]>('${this.apiServerUrl}/student');
正確的代碼是:
this.http.get<Student[]>(`${this.apiServerUrl}/student`);
或者
this.http.get<Student[]>(this.apiServerUrl '/student');
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408187.html
標籤:
