角度2可观察/受试者无法读取属性' next'

时间:2016-12-06 15:19:16

标签: javascript angular observable

我找到了这篇很棒的帖子Creating and returning Observable from Angular 2 Service,现在我尝试做同样的事情,但是得到了这个错误:

  

core.umd.js:2838 EXCEPTION:无法读取属性' next'未定义的

这是我的服务:

@Injectable()
export class ReadBuchungenService {

    public activeProject : ReplaySubject<any>;
    private get_all_buchungen_url : string = "xxxxxxx/xxx/";


    constructor (private http : Http) {}

    public load(timestamp : number) {
        let params = new URLSearchParams();
        params.set('timestamp', String(timestamp));

        this.http.get(this.get_all_buchungen_url, { search: params })
            .subscribe(res => this.activeProject.next(res));
        return this.activeProject;
    }
}

在es6中编译,这里是package.json的依赖项:

"dependencies": {
    "@angular/common": "^2.2.4",
    "@angular/compiler": "^2.1.2",
    "@angular/core": "^2.2.4",
    "@angular/http": "^2.1.4",
    "@angular/platform-browser": "^2.1.2",
    "@angular/platform-browser-dynamic": "^2.1.2",
    "@angular/router": "^3.1.4",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "^0.19.40",
    "zone.js": "^0.6.26"
  }

谢谢!

好的,我现在尝试设置ReplaySubject对象宽度:

public activeProject : ReplaySubject<any> = new ReplaySubject(1);

然后我得到一个意外的令牌&lt;错误,也是在重新安装角度之后。

完整错误的图片:

IMAGE

3 个答案:

答案 0 :(得分:1)

您的activeProject未设置。尝试将其定义为:

public activeProject:ReplaySubject<any> = new ReplaySubject(1);

答案 1 :(得分:0)

您没有初始化activeProject。尝试:

public activeProject : ReplaySubject<any> = new ReplaySubject<any>(1);

答案 2 :(得分:0)

现在我发现了错误,它是来自ReplaySubject类的import语句。

错误:import { Observable, ReplaySubject } from "rxjs";

右:import { ReplaySubject } from 'rxjs/ReplaySubject';

这里是服务的完整代码:

import  {Inject, Injectable} from '@angular/core';
import  { Http, URLSearchParams } from '@angular/http';
import 'rxjs/Rx';
import { ReplaySubject } from 'rxjs/ReplaySubject';


@Injectable()
export class MyService {

    public activeProject: ReplaySubject<any> = new ReplaySubject(1);
    private get_all_buchungen_url: string = "xxxx/xxx";

    constructor(private http: Http) {
    }

    public load(timestamp: number) {

        let params = new URLSearchParams();
        params.set('timestamp', String(timestamp));

        this.http.get(this.get_all_buchungen_url, {search: params})
            .subscribe(res => this.activeProject.next(res));
        console.log(this.activeProject)
        return this.activeProject;
    }
}