栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从一个文件中获取变量到node.js中的另一个文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从一个文件中获取变量到node.js中的另一个文件

您需要的是module.exports

出口产品

在当前模块的所有实例之间共享并可以通过require()访问的对象。export与module.exports对象相同。有关更多信息,请参见src /
node.js。导出实际上不是全局的,而是每个模块的本地性。

例如,如果您想公开

variableName
使用价值
"variablevalue"
sourceFile.js
则可以这样设置整个出口:

module.exports = { variableName: "variablevalue" };

或者,您可以使用以下方法设置单个值:

module.exports.variableName = "variablevalue";

要在另一个文件中使用该值,您

require(...)
首先需要使用它(具有相对路径):

const sourceFile = require('./sourceFile');console.log(sourceFile.variableName);

或者,您可以对其进行解构。

const { variableName } = require('./sourceFile');// current directory --^// ../     would be one directory down// ../../  is 2 directories down

如果你想出来的文件

variableName
,然后

./sourceFile.js:

const variableName = 'variablevalue'module.exports = variableName

./consumer.js:

const variableName = require('./sourceFile')

编辑(2020年):

从开始

Node.js version8.9.0
,您还可以使用具有不同支持级别的ECMAscript模块。该文档

  • 对于Node v13.9.0及更高版本,默认情况下启用实验模块
  • 对于低于版本13.9.0的Node版本,请使用
    --experimental-modules

当传递给节点作为初始输入或被ES模块代码中的import语句引用时,Node.js将把以下内容视为ES模块:

  • 以结尾的文件
    .mjs
  • .js
    在最近的父
    package.json
    文件包含
    "type"
    值为的顶级字段时结束的文件
    "module"
  • 带有标志的字符串作为参数通过STDIN 传递到
    --eval
    or
    --print
    或通过STDIN 传递给node
    --input-type=module


设置完成后,您可以使用

import
export

使用上面的示例,您可以采用两种方法

./sourceFile.js:

// this is a named export of variableNameexport const variableName = 'variablevalue'// alternatively, you could have exported it as a default. // For sake of explanation, I'm wrapping the variable in an object// but it is not necessary. // you can actually omit declaring what variableName is here. // { variableName } is equivalent to { variableName: variableName } in this case. export default { variableName: variableName }

./consumer.js:

// there are three ways of importing. // If you need access to a non-default export, then // you use { nameOfExportedVariable } import { variableName } from './sourceFile'console.log(variableName) // 'variablevalue'// Otherwise, you simply provide a local variable name // for what was exported as default.import sourceFile from './sourceFile'console.log(sourceFile.variableName) // 'variablevalue'

./sourceFileWithoutDefault.js:

// The third way of importing is for situations where there// isn't a default export but you want to warehouse everything// under a single variable. Say you have:export const a = 'A'export const b = 'B'

./consumer2.js

// then you can import all exports under a single variable// with the usage of * as:import * as sourceFileWithoutDefault from './sourceFileWithoutDefault'console.log(sourceFileWithoutDefault.a) // 'A'console.log(sourceFileWithoutDefault.b) // 'B'// you can use this approach even if there is a default export:import * as sourceFile from './sourceFile'// default exports are under the variable default:console.log(sourceFile.default) // { variableName: 'variablevalue' }// as well as named exports:console.log(sourceFile.variableName) // 'variablevalue


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号