我发现了这篇很棒的帖子Creating and returning Observable from Angular 2 Service现在我尝试做同样的事情,但得到这个错误:
core.umd.js:2838 EXCEPTION: Cannot read property 'next' of undefined
这是我的服务:
@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);
但随后我得到了一个意外的 token <错误,也是在全新安装 Angular 之后。
完整错误的图像:
最佳答案
您的 activeProject 尚未设置。尝试将其定义为:
public activeProject:ReplaySubject<any> = new ReplaySubject(1);
关于javascript - Angular 2可观察/主题无法读取属性 'next',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998770/