【问题标题】:RxJS lettable operators within Angular CLIAngular CLI 中的 RxJS 可出租运算符
【发布时间】:2017-11-02 18:08:34
【问题描述】:

拥有使用 Angular CLI 1.5.0 生成的 Angular 5 应用程序,我想使用 lettable 运算符。但是,简单的用法失败了:

import { Component } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
// import 'rxjs/add/operator/mergeMap';
import { mergeMap } from 'rxjs/operators';

@Component({
  selector: 'foo-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title: Observable<string>;

  constructor() {
    this.title = Observable.of(['Hello', 'World']).mergeMap(x => Observable.of(x.join(' ')));
  }
}

生产:

文件:'file:///c%3A/Users/drago/fooGui-1.5.0/src/app/app.component.ts' 严重性:'错误'消息:'属性'mergeMap'在类型上不存在 '可观察的'。在:'16,52' 来源:'ts'

为了在 Angular CLI 中使用 lettable 操作符,是否还需要进行其他配置?

谢谢

【问题讨论】:

标签: angular rxjs angular-cli


【解决方案1】:

我相信您需要使用 pipe 运算符来调用可出租运算符 ref

import { mergeMap } from 'rxjs/operators';
this.title = Observable.of(['Hello', 'World']).mergeMap(x => Observable.of(x.join(' ')));

改成

import { mergeMap } from 'rxjs/operators';
this.title = Observable.of(['Hello', 'World']).pipe(
  mergeMap(x => Observable.of(x.join(' ')))
);

顺便说一句,mergeMapObservable.of 不会互相抵消吗?所以,

this.title = Observable.of(['Hello', 'World']).pipe(
  map(x => x.join(' '))
);

【讨论】:

  • 这只是一个虚拟示例,用于测试
  • 确实如此,但值得一提的是,以防有人复制该模式(我不会注意到,只是昨天在另一个问题中出现了)。
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
相关资源
最近更新 更多