我是网络开发的新手,我想知道如何使前端的角项目与后端角项目进行通信。
发布于 2019-08-09 22:33:25
如果我正确理解你的问题,你想要发送和接收数据从你的后端角度应用程序。
以角度获取数据:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
}getConfig() {
return this.http.get(this.configUrl);
}showConfig() {
this.configService.getConfig()
.subscribe((data: Config) => this.config = {
heroesUrl: data['heroesUrl'],
textfile: data['textfile']
});
}向服务器发送数据
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}this.heroesService
.addHero(newHero)
.subscribe(hero => this.heroes.push(hero));有关更多细节,请通过角度HttpClient指南。
https://stackoverflow.com/questions/57437725
复制相似问题