简体   繁体   English

如何在节点 js 中正确导入/导出方法

[英]How to properly import/export a method in node js

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.我目前正在尝试从 class 导入一个方法,以保持一切整洁,因为我计划导入的方法会很长。 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.编辑:如果我简单地结合两个类并将方法放在 ImportingClass.js 中,代码就可以工作,但是代码太多了。

ValidateInput.js:验证输入.js:

class ValidateInput
{
    validateInput(event)
    {
        var ID = event.target.StudentID.value;
        // ...
    }
}

ImportingClass.js:导入类.js:

import {validateInput} from '../ValidateInput';

class ImportingClass
{

    handleSubmit(event)
    {
        validateInput(this.event);
        // ...
    }
}

Error: Uncaught TypeError: (.validateInput) is not a function错误:未捕获的类型错误:(.validateInput)不是 function

But we can do something like this as well但我们也可以这样做

create an independent function validateInput创建独立的 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 中调用这个 function 是ValidateInput.js我们可以做这样的事情

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'在您的第二个文件ImportingClass.js中,您可以导入并使用import {validateInput} from './validateInput'

or you can export it via Prototype something like this或者您可以通过类似这样的原型导出它

export const validateInput = ValidateInput.prototype.validateInput
  1. You'll need to export the class so it can be called externally in a different module.您需要导出 class 以便可以在不同的模块中从外部调用它。 The way to do this is by adding your class to the exports object.方法是将 class 添加到导出 object 中。

ValidateInput.js:验证输入.js:

class ValidateInput
{
    validateInput(event)
    {
        var ID = event.target.StudentID.value;
        // ...
    }
}

exports.ValidateInput = ValidateInput;
  1. Import your ValidateInput class in the module you would like to use it in.将您的ValidateInput class 导入您想要使用的模块中。

ImportingClass.js:导入类.js:

// import {validateInput} from '../ValidateInput';
const fileName = require(../ValidateInput.js);

class ImportingClass
{

    handleSubmit(event)
    {
        fileName.ValidateInput.validateInput(this.event);
        // ...
    }
}

I've used the namespace fileName to avoid confusion.我使用了命名空间fileName来避免混淆。 It refers to the file.它指的是文件。 The first property accessed is your class name, then the method you want to call.访问的第一个属性是您的 class 名称,然后是您要调用的方法。 It can be a bit confusing if you are using virtually the same name for the file, class and method name.如果您对文件使用几乎相同的名称 class 和方法名称,这可能会有点混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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