Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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 运算符函数<;未知,未知>;类型';中缺少以下属性;可观察的';:_isScalar,来源,操作员,_Angular_Typescript_Observable - Fatal编程技术网

Angular 运算符函数<;未知,未知>;类型';中缺少以下属性;可观察的';:_isScalar,来源,操作员,

Angular 运算符函数<;未知,未知>;类型';中缺少以下属性;可观察的';:_isScalar,来源,操作员,,angular,typescript,observable,Angular,Typescript,Observable,在Angular中为HTTP请求实现getProducts()方法时遇到问题。我将observable设置为匹配我的接口observable,以从productUrL获取并返回JSON文件中的产品,但它会抛出一个错误: Type 'OperatorFunction<unknown, unknown>' is missing the following properties from type 'Observable<IProduct[]>': _isScalar, so

在Angular中为HTTP请求实现getProducts()方法时遇到问题。我将observable设置为匹配我的接口
observable
,以从
productUrL
获取并返回JSON文件中的产品,但它会抛出一个错误:

Type 'OperatorFunction<unknown, unknown>' is missing the following properties from 
type 'Observable<IProduct[]>': _isScalar, source, operator, lift, and 6 more.  
下面是
produst list.component
,它正在调用方法
onInit

 errorMessage: string;
 products: IProduct[] = [];

  ngOnInit() {
    this.productService.getProducts().subscribe({
      next: products => this.products = products,
      error: err => this.errorMessage = err
    });

您得到的错误很可能是因为这一行:

tap(data => console.log("All: " + JSON.stringify(data))))
您可以使用箭头函数(或匿名函数),但必须记住,如果您没有将代码放入括号
{}
,则表示您正在返回此代码的结果。 这意味着,您的代码与以下代码相同:

tap(data => {
return console.log("All: " + JSON.stringify(data)))
   }
);
我也在谈论这一点:

  next: products => this.products = products,
  error: err => this.errorMessage = err
试试这个,乍一看,catchError应该在管道内

  next: products => this.products = products,
  error: err => this.errorMessage = err
.pipe(
  tap(data => console.log("All: " + JSON.stringify(data))),
  catchError(this.handleError)
)