I am currently trying to import a method from a class to keep everything tidy since the method I plan to import will be quite long. However, I seem to get a new error every time I switch it up. The biggest issue is the import/export and the "event" parameter that don’t seem to want to work with me.
Edit: The code works if I simply combine both classes and put the method in ImportingClass.js, however it would be too much code.
ValidateInput.js:
class ValidateInput
{
validateInput(event)
{
var ID = event.target.StudentID.value;
// ...
}
}
ImportingClass.js:
import {validateInput} from '../ValidateInput';
class ImportingClass
{
handleSubmit(event)
{
validateInput(this.event);
// ...
}
}
Error: Uncaught TypeError: (.validateInput) is not a function
>Solution :
But we can do something like this as well
create an independent function validateInput
export function validateInput(e){
.... code
}
and in order to call this function in your first class that is ValidateInput.js
we can do something like this
class ValidateInput{
validateInput(){
validateInput.apply(this, ...args)
}}
and in your second file ImportingClass.js you can import and use it
with import {validateInput} from './validateInput'
or you can export it via Prototype something like this
export const validateInput = ValidateInput.prototype.validateInput