【问题标题】:How do I resolve an Angular/rxjs/observable error?如何解决 Angular/rxjs/observable 错误?
【发布时间】:2020-03-18 22:01:39
【问题描述】:

参考:角度,可观察,rxjx 6.4.0

我的代码如下:

import { Observable } from 'rxjs';
import 'rxjs/add/observable/of';
...
return Observable.of(this.products);
...

错误显示:

Property 'of' does not exist on type 'typeof Observable'.

我也可以从导入中删除 /add,因为 of.js 存在于两个文件夹中。但这会返回相同的错误。

【问题讨论】:

标签: angular rxjs observable


【解决方案1】:

您的代码应如下所示:

 import { Observable, of } from 'rxjs'

 return of(this.products);

【讨论】:

    【解决方案2】:

    正如其他人所指出的,在该版本的 RxJs 中,静态方法和运算符的导入方式不同。

    这是他们docs的一个例子:

    import { of } from 'rxjs';
    
    of(10, 20, 30)
    .subscribe(
      next => console.log('next:', next),
      err => console.log('error:', err),
      () => console.log('the end'),
    );
    // result:
    // 'next: 10'
    // 'next: 20'
    // 'next: 30'
    

    所以你的代码看起来像:

    import { of } from 'rxjs';
    
    ...
    return of(this.products);
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2021-11-02
      • 2019-07-16
      • 2019-06-09
      • 2018-07-05
      • 2019-08-17
      • 1970-01-01
      相关资源
      最近更新 更多