Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 返回错误。订阅角度5_Angular - Fatal编程技术网

Angular 返回错误。订阅角度5

Angular 返回错误。订阅角度5,angular,Angular,我想发送返回错误时,我的功能失败,但我不知道我怎么做 服务台 math(){ try { . . . } catch (error) { return "error"; } } calc(url){ num = math(); if (num !== "error"){ return this.http.get<Object[]

我想发送返回错误时,我的功能失败,但我不知道我怎么做

服务台

 math(){
     try {
             .
             .
             .
       } catch (error) {
          return "error";
      }
    }

calc(url){
  num  = math();
  if (num !== "error"){
    return this.http.get<Object[]>(url);
  } else {
      return Error;
  }
}

我在
this.service.calc(url).subscribe中得到错误,因为这不允许->
从calc返回错误
使用
throwerr
来观察返回

import { throwError } from 'rxjs';

calc(url){
  num  = math();
  if (num !== "error"){
    return this.http.get<Object[]>(url);
  } else {
      return throwError('Error');
  }
}
从'rxjs'导入{throwError};
计算(url){
num=数学();
如果(num!=“error”){
返回此.http.get(url);
}否则{
返回投掷器(“错误”);
}
}

你能发布一个Stackblitz/粘贴你的
数学方法的完整代码吗?我认为最好使用
投掷者
而不是
。所以订户知道这是个错误。
import { throwError } from 'rxjs';

calc(url){
  num  = math();
  if (num !== "error"){
    return this.http.get<Object[]>(url);
  } else {
      return throwError('Error');
  }
}