这是我的第一份文件:
var self = this;
var config = {
'confvar': 'configval'
};
我想把这个配置变量放在另一个文件中,所以我在另一个文件中这样做了:
conf = require('./conf');
url = conf.config.confvar;
但这给了我一个错误.
TypeError:无法读取未定义的属性"confvar"
我能做什么?
这是我的第一份文件:
var self = this;
var config = {
'confvar': 'configval'
};
我想把这个配置变量放在另一个文件中,所以我在另一个文件中这样做了:
conf = require('./conf');
url = conf.config.confvar;
但这给了我一个错误.
TypeError:无法读取未定义的属性"confvar"
我能做什么?
从Node.js version 8.9.0开始,您还可以使用具有不同支持级别的ECMAScript模块.The documentation
--experimental-modules
node .当js在ES中被引用时,将以下语句视为输入:
- 文件以
.mjs
结尾.- 当最近的父文件
package.json
包含值为"module"
的顶级字段"type"
时,文件以.js
结尾.- 字符串作为参数传入
--eval
或--input-type=module
.
一旦设置好,就可以使用import
和export
.
通过上面的例子,你可以采取两种方法
// This is a named export of variableName
export 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 }
// 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'
// 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'
// 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
您可以从另一个文件重新导出任何内容.当目录中只有一个出口点(index.{ts|js}
)但有多个文件时,这很有用.
假设您有以下文件夹 struct :
./src
├── component
│ ├── index.js
│ ├── myComponent.js
│ └── state.js
└── index.js
你们可以从这两家store 购买各种出口商品.js和我的组件.但只想导出其中的一部分.
import createState from "./state";
export function example(){ };
export default function() {}
export { example as default } from "./myComponent";
export * from "./myComponent"
export * from "./component"
你需要module.exports:
出口
在当前模块的所有实例之间共享的对象
例如,如果希望在sourceFile.js
上显示值为"variableValue"
的variableName
,则可以将整个导出设置为:
module.exports = { variableName: "variableValue" };
Or您可以使用以下选项设置单个值:
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 two directories down
如果你只想从文件中得到variableName
,那么
const variableName = 'variableValue'
module.exports = variableName
const variableName = require('./sourceFile')