0

I want to call a function from an imported class. This is my code

import test from '../test'
test.showMessage();  // This is not working

this is the content of the test component

export default class test {
showMessage(){
alert('Some text');
}
}

I get an error telling me that showMessage is not a function. How can I solve this?

2 Answers 2

1

You have to make showMessage() static :

export default class test {
  static showMessage(){
    alert('Some text');
  }
}

Let me know if this worked for you.

Sign up to request clarification or add additional context in comments.

Comments

0

You can either make the method static like @Himanshu suggested, or you can initiate an instance of your class:

import test from '../test'
const myTest = new test()

myTest.showMessage();  // This works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.