我读了它:angular-cli server - how to proxy API requests to another server?
但是该解决方案对我不起作用。
Angular在localhost:4200
上工作,变量msg
应该从localhost:8080/msg
中获取。
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'netty-board-frontend';
msg = 'not found msg';
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.http.get('/api/msg', {responseType: 'text'}).subscribe(data => {
this.msg = data as string;
});
}
}
我已创建proxy.conf.json
文件:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
并放入package.json
:
"start": "ng serve --proxy-config proxy.config.json",
但变量msg仍来自http://localhost:4200/api/msg
而不是http://localhost:8080/msg
。
Request URL: http://localhost:4200/api/msg
Request Method: GET
Status Code: 404 Not Found
Remote Address: 127.0.0.1:4200