我有服务:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Order} from './interfaces/Order';
import {OrderResponse} from './interfaces/OrderResponse';
import {AuthorizeRequest} from './interfaces/AuthorizeRequest';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
@Injectable({
providedIn: 'root'
})
export class OrderService {
constructor(private http: HttpClient) {}
public addOrder(newOrder: Order){
return this.http.post<OrderResponse>('http://localhost:8080/order/add', newOrder, httpOptions)
}
}
订购组件:
import { Component, OnInit } from '@angular/core';
import {Order} from '../interfaces/Order';
import {OrderService} from '../order.service';
import {OrderResponse} from '../interfaces/OrderResponse';
@Component({
selector: 'app-orderpage',
templateUrl: './orderpage.component.html',
styleUrls: ['./orderpage.component.css']
})
export class OrderpageComponent implements OnInit {
public order: Order;
public orderResponse: OrderResponse;
constructor(private orderService: OrderService) { }
ngOnInit() {
this.order = {
dateOfService: '',
city: '',
address: '',
apartmentSize: 0,
phoneNumber: '',
status: "ORDERED",
client: null
};
this.orderResponse = {
orderId: 0,
price: 0
};
}
public addOrder(){
this.orderService.addOrder(this.order).subscribe(response => {
this.orderResponse = response;
console.log(response);
});
}
}
当我发送发布请求时,后端向我返回OrderResponse。我可以在console.log(response)中显示此数据,但不能保存到接口orderResponse。问题是什么? orderResponse始终为空。
答案 0 :(得分:0)
在此功能中:
public addOrder(){
this.orderService.addOrder(this.order).subscribe(response => {
this.orderResponse = response;
});
}
当我在.subscribe(...)中检查this.orderResponse时,我具有很好的价值。 但是我想保存此orderReponse以便在其他组件中使用它。我该怎么办?