【问题标题】:Property 'next' does not exist on type 'Observable<any>''Observable<any>' 类型上不存在属性 'next'
【发布时间】:2017-12-05 13:12:22
【问题描述】:

我正在学习 Angular 5,我一直在尝试使用 .next 方法添加 data.service.ts 在尝试这个:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()

export class DataService {

  private go = new BehaviorSubject<any>([' First Goal','Second Goal']);

  newvar = this.go.asObservable();

  constructor() { }

  changeGoal(newvar){
    this.newvar.next(this.go);
  }

}

我得到了这个错误:“属性'next'不存在于类型'Observable'上”;

【问题讨论】:

  • 我发现这个解决方案只需更改一行“this.go.next(this.newvar);”
  • 供以后参考,错误的原因是next(...)是RxJS主题的​​成员,而不是可观察的。这就是您的上述解决方案有效的原因。

标签: angular5


【解决方案1】:

由于.next()Subject 而不是Object 的属性,因此您应该使用go.next() 而不是this.newvar.next()

changeGoal(newvar) {
    this.go.next(value);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-20
    • 2016-09-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2019-01-25
    • 2018-11-16
    相关资源
    最近更新 更多