Before the es6 standard came out, whatever was exported was what was required.
Take nodejs as an example
// ./fk.js export 'fk'; // ./main.js var fk = require('./fk'); console.log(fk); // output "fk"
Look, this is not "modular" at all. Since the module is imported, it should have values, functions, cars, donkeys, and hot pots.
What to do? The label stipulates that all exported content must have a "name". You can also give it an alias, but it must have a name anyway!
The first way to choose a name
export var/let/const 名字=值
Second method of naming
var/let/const 变量=值 export {变量} 或 export {变量 as 别名}
The third way to choose a name
export default 值
The second method in the first picture reports an error because you don’t use curly brackets. When compiling,mI don’t know what it’s called. If I don’t know what it’s called, I don’t have a name. I can’t come up with a name. It cannot be exported.
export { name1, name2, …, nameN }; export { variable1 as name1, variable2 as name2, …, nameN }; export let name1, name2, …, nameN; // also var export let name1 = …, name2 = …, …, nameN; // also var, const export default expression; export default function (…) { … } // also class, function* export default function name1(…) { … } // also class, function* export { name1 as default, … }; export * from …; export { name1, name2, …, nameN } from …; export { import1 as name1, import2 as name2, …, nameN } from …;
The two forms you wrote are not included...For the specific meaning of each form, please read the document!
For this kind of problem, just read the document directly. And didn’t you take this picture from Ruan Yifeng’s article? The original text has clear instructions
It is important to note that the export command specifies the external interface and must establish a one-to-one correspondence with the variables inside the module.
The above two writing methods will report errors because no external interface is provided. The first way of writing directly outputs 1, and the second way of writing directly outputs 1 through variable m. 1 is just a value, not an interface.
Before the es6 standard came out, whatever was exported was what was required.
Take nodejs as an example
Look, this is not "modular" at all. Since the module is imported, it should have values, functions, cars, donkeys, and hot pots.
What to do?
The label stipulates that all exported content must have a "name". You can also give it an alias, but it must have a name anyway!
The first way to choose a name
Second method of naming
The third way to choose a name
The second method in the first picture reports an error because you don’t use curly brackets. When compiling,
m
I don’t know what it’s called. If I don’t know what it’s called, I don’t have a name. I can’t come up with a name. It cannot be exported.End.
See the documentation export on MDN
There are only a few forms in total
The two forms you wrote are not included...For the specific meaning of each form, please read the document!
https://developer.mozilla.org...
For this kind of problem, just read the document directly. And didn’t you take this picture from Ruan Yifeng’s article? The original text has clear instructions