V. Aliosha
V. Aliosha

Reputation: 152

cant call function from other class

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.

Upvotes: 1

Views: 105

Answers (2)

Bergi
Bergi

Reputation: 664538

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.

Upvotes: 1

eko
eko

Reputation: 40647

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

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

Upvotes: 3

Related Questions