ngc:错误:导出类的公共方法的返回类型具有或正在使用名称' Observable'来自外部模块但无法命名

时间:2016-10-11 00:34:13

标签: angular

代码

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?我无法用我迄今为止所尝试的内容进行编译。

2 个答案:

答案 0 :(得分:1)

嗯,这有点奇怪,但我设法用服务中的私有方法修复它: 放置this.store.select(state => state.user)方法的位置,如果您使用的是import {Http} from "@angular/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以使其正常工作,如果您能提供服务将更好地找出问题。

答案 1 :(得分:1)

我必须将{{1}}放入返回任何Observable的公共函数中,如果这些是从导入的类或服务返回的话。