我在Angular 4中有这个方法:
modifyStock(id: number, quantity: number) {
const url = `/item/${id}/modifyStock`;
const body = { quantity: quantity };
return this.http.put(url, body);
}
Run Code Online (Sandbox Code Playgroud)
我想控制是否有任何输入参数,如果发生null则不调用,然后抛出一个必须在调用此参数的方法中捕获的错误。http.put()subscribe()
我尝试过这个和一些组合throw Observable.throw('Error'),但我无法让它工作。
modifyStock(id: number, quantity: number) {
if (id == null || quantity == null) {
return Observable.throw('Error');
}
const url = `/item/${id}/modifyStock`;
const body = { quantity: quantity };
return this.http.put(url, body);
}
Run Code Online (Sandbox Code Playgroud)