typescript 错误 TS4053:来自导出类的公共方法的返回类型具有或正在使用名称“Observable”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39870881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error TS4053: Return type of public method from exported class has or is using name ‘Observable’
提问by Manspof
i'm trying to build app with ionic 2 & angular 2, i get this error while i try to run my app. i build another project to check and the same problem, i really confused on this problem.
我正在尝试使用 ionic 2 和 angular 2 构建应用程序,当我尝试运行我的应用程序时出现此错误。我建立了另一个项目来检查同样的问题,我真的对这个问题感到困惑。
this is my service code
这是我的服务代码
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Storage} from '@ionic/storage';
import {NavController} from "ionic-angular";
/*
Generated class for the MyService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MyService {
public local :Storage;
public getsession : any;
constructor(private http: Http, private navCtrl : NavController) {
this.local = new Storage();
console.log("my-service page")
}
postLogin(data){
let link = "http://adirzoari.16mb.com/login.php";
return this.http.post(link,data)
.map(res => res.json())
}
checkToken(){
return this.getsession =this.local.get('token');
}
}
回答by sebaferreras
I only add this as an answer so it could help other SO users facing the same issue.
我仅将此添加为答案,以便它可以帮助面临相同问题的其他 SO 用户。
Just like @sudheer-kbmentioned, in order to solve that issue, you need to explicitlyimport the Observable
class:
就像@sudheer-kb提到的,为了解决这个问题,你需要显式导入Observable
类:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
// ...
import { Observable } from "rxjs/Observable"; // <- add this import
And then give your method an explicit return type (also thanks @ruffinfor your comment):
然后给你的方法一个明确的返回类型(也感谢@ruffin的评论):
postLogin(data): Observable<any> {
// ...
}
回答by Buisson Réda
I had a similar issue, it seems related to a problem with return types of methods. What worked for me is to simply add ": any" just after declaration of my methods, like this for example :
我有一个类似的问题,它似乎与方法返回类型的问题有关。对我有用的是在我的方法声明之后简单地添加“:任何”,例如:
get(url) : any {
//code
}
I don't think it's a good habit, but it can be a good fix sometimes.
我不认为这是一个好习惯,但有时它可以是一个很好的解决方法。