각도 http.post, .dv 콜백 없음
콜백에 가입하지 않고 http 포스트 요청만 할 수 있을까, 이런 거.
this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null);
이것 대신에
this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null)
.subscribe();
난 네가 할 수 있다고 생각하지 않아.
http.post (그리고 get, put, delete 등)은 관측 가능한 콜드(Cold Observable)를 반환하며, 이는 다음을 위한 관측 가능한 것이다.
구독 기간 동안 기본 프로듀서가 생성 및 활성화됨
출처.
즉, 로 대표되는 함수를 의미한다.Observable
에 의해서만 활성화된다.subscribe()
방법
편의 방식도 구독 가능, 구현 세부 정보 참조Observable#toPromise()
여기
약속으로 변환하는 중(rxjs 필요):
import 'rxjs/add/operator/toPromise';
@Injectable()
export class SomeService {
....
post(sp: Seatplace, date?: Date) : Promise<any> {
return this.http.post(
'/list/items/update?itemId=' + itemId + "&done=" + done,
null
).toPromise();
}
}
나는 같은 질문을 받았지만, 나는 누군가가 관찰 가능한 것을 구독해도 상관 없다는 것을 알게 되었다.나는 어떤 경우에도 POST 요청을 보내고 싶다.내가 생각해 낸 것은 다음과 같다.
postItem(itemData) {
var observable = this.http.post('/api/items', itemData)
.map(response => response.json()) // in case you care about returned json
.publishReplay(); // would be .publish().replay() in RxJS < v5 I guess
observable.connect();
return observable;
}
요청은 즉시 전송된다.connect()
라고 불린다.그러나 아직도 발신자가 관측할 수 있는 것은 있다.postItem
필요한 경우 구독할 수 있다.이후publishReplay()
단지 대신에 사용된다.publish()
, POST 요청이 완료된 후에도 가입이 가능하다.
@picci points처럼 규칙적인 관찰은 차가운 관찰이다.@lex82 아이디어를 시도해 볼 수 있는 요청을 하려면 rxjs 6 초안을 작성하십시오.
import { ConnectableObservable } from "rxjs"
import { publish } from "rxjs/operators";
const myConnectableObservable: ConnectableObservable<any> = this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null).pipe(publish()) as ConnectableObservable<any>;
myConnectableObservable.connect();
그것은 기본적으로 구독과 같지만 실제 구독을 하지 않는 것이다.
https://blog.danlew.net/2018/09/25/connectable-observables-so-hot-right-now/
다음과 같은 Promise 방법을 간단히 사용할 수 있다.
public getEmployerListFromService() {
const url = `/list/items/update?itemId=`;
return this.http.get<any>(url).toPromise().then(response => {
console.log(response);
})
}
그리고 이 메서드를 다음으로 호출하십시오.
this.service.getEmployerListFromService();
참조URL: https://stackoverflow.com/questions/41296052/angular-http-post-without-subscribe-callback
'Programing' 카테고리의 다른 글
VueJ의 구성 요소 템플릿에서 Vuetify 대화 상자 열기s (0) | 2022.03.10 |
---|---|
reactive native - 구성 요소 클래스가 예상됨, [Object] 확인 (0) | 2022.03.10 |
VueRouter 인스턴스에서 경로 개체에 액세스하는 중 (0) | 2022.03.10 |
TDD 중 Vue 인스턴스의 조롱 방법 (0) | 2022.03.10 |
조건에 따라 모든 경로가 동적으로 로드될 때 경로로 리디렉션하는 방법? (0) | 2022.03.10 |