码
this.store.select(state => state.user).subscribe(u => user = u).unsubscribe();
https://gist.github.com/nottinhill/eac309590096cc6f1b910f40a1b2f0c3
https://gist.github.com/nottinhill/5dc4026007a0c3ea3a4e0c15c0adfe05
错误
[02:25:41] ngc: Error: Error at /Users/tyrion/devel/saveup-front/.tmp/+purchase/shared/purchase-service/purchase.service.ts:22:12: Return type of public method from exported class has or is using name 'Observable' from external module "/Users/tyrion/devel/saveup-front/node_modules/rxjs/Observable" but cannot be named.
问题
强类型在哪里输入Observable?我无法用我迄今为止所尝试的内容进行编译.
最佳答案 嗯,这有点奇怪,但我设法用私有方法在服务中修复它:
你在哪里放置this.store.select(state => state.user)方法,如果你使用来自“@ angular / http”的import {Http};确保你在附近做了些什么:
import {Injectable} from '@angular/core';
import {Http, Response} from "@angular/http";
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {Observable} from "rxjs";
@Injectable()
export class MyService {
static get parameters() {
return [[Http]];
}
constructor(private http: Http) {
}
getList() {
var url = 'http://<URL_HERE>';
var response = this.http.get(url).map(this.extractData).catch(this.handleError);
console.log(response);
return response;
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
我添加了这个方法extractData以使其工作,如果你能提供你的服务将更好地找出问题.