1

I have 2 files.

1) accountService.js

export class Http {

forgotPassword(email) {
console.log(email)
  }
}

2) forgot-password.js

import {Http} from '../services/accountService'

 export class ForgotPassword {

 sendCode(email) {
    Http.forgotPassword(email)
  }
}

When I'm trying to call Http.forgotPassword(email) in forgot-password.js there is console error, that says Http.forgotPassword in not a function.

V. Aliosha
  • 152
  • 1
  • 1
  • 14

2 Answers2

3

forgotPassword method needs to be static if you want to call it like that;

static forgotPassword(email) {
    console.log(email)
}
eko
  • 39,722
  • 10
  • 72
  • 98
1

In your example, forgotPassword is an instance method. You will need to do

export class ForgotPassword {
  constructor() {
    this.http = new Http
  }
  sendCode(email) {
    this.http.forgotPassword(email)
  }
}

However, if those files you've shown were the whole code, you should neither use classes nor export objects with methods. Just export the functions.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375