简体   繁体   中英

Typescript Error Expected 0 arguments, but got 2 in Ionic Framework

The typescript compiler gives me this error

typescript: src/pages/login/login.ts, line: 33 Expected 0 arguments, but got 2. L32: this.auth.loginAPI(this.registerCredentials) L33: .subscribe(response => {

With this code (2 files):

// auth-provider.ts
public loginAPI(credentials: {email:string,password:string}) {
  return this.http.post(this.apiUrl,data);
}

// login.ts
this.auth.loginAPI(this.registerCredentials)
  .subscribe(response => { 
    console.log(response);
  }, err => {
    console.log(err);
  });

The funny thing is that the code works (I edit any file, my ionic serve reloads and the page renders without any problem).

Any idea?

Environment cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.19.0
ionic (Ionic CLI) : 3.19.0

global packages:

cordova (Cordova CLI) : 7.0.1

local packages:

@ionic/app-scripts : 3.1.5
Cordova Platforms  : ios 4.4.0
Ionic Framework    : ionic-angular 3.9.2

System:

Node  : v8.9.3
npm   : 5.5.1
OS    : macOS High Sierra
Xcode : Xcode 9.2 Build version 9C40b

The problem is with the loginAPI function definition. Adding the return type to Observable<any> made the compiler happy and solved the issue.

// auth-provider.ts
public loginAPI(credentials: {email:string,password:string}):Observable<any> {
  return this.http.post(this.apiUrl,data);
}

Still I don't really understand very well why, but at least now it compiles without any error.

In order to handler this you can create a class as follows,

public class credentials export {
   email : string;
   password: string;
}

and then,

public loginAPI(inCredentials : credentials ) {
  return this.http.post(this.apiUrl,data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM