Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error TS4053: Return type of public method from exported class has or is using name ‘Observable’

Tags:

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.

error in photo

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');
  }

}
like image 926
Manspof Avatar asked Oct 05 '16 09:10

Manspof


2 Answers

I only add this as an answer so it could help other SO users facing the same issue.

Just like @sudheer-kb mentioned, in order to solve that issue, you need to explicitly import the Observable class:

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 @ruffin for your comment):

postLogin(data): Observable<any> {
    // ...
}
like image 154
sebaferreras Avatar answered Sep 22 '22 22:09

sebaferreras


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.

like image 27
Buisson Réda Avatar answered Sep 19 '22 22:09

Buisson Réda