【问题标题】:angular 2 Observable / Subjects cannot read property 'next'angular 2 Observable / Subjects 无法读取属性“下一个”
【发布时间】:2017-04-21 07:02:15
【问题描述】:

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

core.umd.js:2838 异常:无法读取未定义的属性“下一个”

这是我的服务:

@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);

但是我得到一个 Unexpected token

完整错误的图片:

【问题讨论】:

    标签: javascript angular observable


    【解决方案1】:

    现在我发现了错误,它是来自 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 当我设置它时,与“Shai”答案中评论的错误相同。 “错误:(SystemJS)意外的令牌
      • 如果您在该错误消息中单击展开 (...),您可以获得更多信息。它可能会显示带有意外
      • 确定:(索引):16 错误:(SystemJS)意外令牌
      • 这并没有多大帮助,不是还有更多的三个点可以点击,以获得更深层次的例外吗? i1.kym-cdn.com/photos/images/facebook/000/531/557/a88.jpg
      【解决方案3】:

      你没有初始化activeProject。试试:

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

      【讨论】:

      • 我已经尝试过了,但是我得到了这个错误:错误:(SystemJS)意外的令牌
      • 删除一个,不确定是哪个
      • 您是否尝试删除它们?
      猜你喜欢
      • 2016-11-14
      • 2017-10-10
      • 2018-09-02
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2019-02-12
      相关资源
      最近更新 更多