The distinction between correlation and export, export default, module.exports, import, require

# Module.exports Node application is composed of modules, using CommonJS module specification. According to this specification, each file is a module, has its own scope. Variable in these documents which defined functions, classes are private, not visible outside, and therefore circumvented the scope of pollution. The predetermined CommonJS, each internal module, module variables representing the current module, this variable is an object, it exports properties (i.e. module.exports) is an external interface. ** load a module, in fact, exports to load the properties of the module. ** Example: by age and sayHelloTo module.exports output variable function. ./MyModule.js `` `javascript var age = 7; var sayHelloTo = function (name) {return" hello "+ name;}; module.exports.age = age; module.exports.sayHelloTo = sayHelloTo;` `` # require: a load module `` `javascript var temp = require ( './ MyModule.js'); // can be used here import myModule from './MyModule.js' console.log (temp.age); // 7 console.log (temp.sayHelloTo ( "Steve")); // hello Steve `` `> additional Note: for custom modules, need to use a relative path, otherwise it will prompt can not find the module / component (default the case, citing a non-relative path, looks from node_modules folder) # exports and module.exports for convenience, node provides a variable for each module exports, pointing module. exports. This is equivalent to the head of each module, such a line of code: `` `javascript var exports = module.exports;` `` Thus, you can add a method exports directly on the object (equivalent to adding the same in module.exports) ./MyModule.js `` `javascript var age = 7; var sayHelloTo = function (name) {return" hello "+ name;}; exports.age = age; // equivalent to: module.exports.age = age ; exports.sayHelloTo = sayHelloTo; // equivalent to: module.exports.sayHelloTo = sayHelloTo; `` `** PS exports can not directly point to a value, which would cut off the module.exports exports (but may be module .exports to point to a value) ** ./MyModule.js `` `javascript var age = 7; var sayHelloTo = function (name) {return" hello "+ name;}; exports = age; // do not do that. Doing so will cut off exports of the module.exports `` `# differs from CommonJS, ES6 use export and import to import, export, export module with module export, you need to use import to import, rather than using require. PS: export order requiring the external interface is necessary to establish one to one relationship with the internal module variable `` `javascript const utils = {showSth: function () {console.log (" showSth "); }, SaySth: function () {console.log ( "saySth");}} / * 3 ways derived * / export var m = utils; // Embodiment 1, so that when this approach requires references: import {m} from './utils.js'; export {utils}; // embodiment 2, with braces to derive variables, if a plurality of variable derived, then {variable 1, variable 2, 3 ... variable , the variable N}. This approach requires when referring to such: import {utils} from './utils.js'; export {utils as myUtils}; // embodiment 3, so that when this approach requires references: import {myUtils} from './utils.js'; where cited, can directly specify an alias, such as: import {myUtils as utils} from './utils.js'; `` `# MDN for export and import syntax description: export syntax: `` `javascript export {name1, name2, ..., nameN}; export {variable1 as name1, variable2 as name2, ..., nameN}; export let name1, name2, ..., nameN; // also var, const export let name1 = ..., name2 = ..., ..., nameN; // also var, const export function FunctionName () {...} export class ClassName {...} export default expression; export default function (...) {...} / / also class, export and export default can be used to derive (constant | function | file | module) and so on. Import + 2 by way of the other files (const | function module | | file) name, which was introduced in order to enable use. 3. In a file or module, export, import can have multiple, but only one export default. `` `Javascript const utils = {showSth: function () {console.log (" showSth ");}, saySth: function () {console.log (" saySth ");}} const name =" my name is Artech "; export {name}; // name deriving export {utils}; // for naming derived, must use the same name when a corresponding object reference at the time of import: import {utils, name as myName} from '. /utils.js'; `` `4. derived by way of export, import use in braces {}; and export by way of default derived is not required:` `` javascript // default as derived by export export default utils; // then use the time without any increase in braces, and name when importing can be customized, such as: import myUtils from './utils.js'; `` `- for the default exported, the imported time, names can be taken lightly. - Default export: You can not let, var or const as the default export. # Import * js the method defined in the file, module, object, etc., all derived, generally in combination aliasing, such as: myModule.

Guess you like

Origin www.cnblogs.com/deepthought/p/exportexport-defaultmoduleexportsimportrequire-zhi.html