Javascript 如何导出用于在ES6中导入的函数?
不知道我在节点v10.5.0中做错了什么Javascript 如何导出用于在ES6中导入的函数?,javascript,node.js,Javascript,Node.js,不知道我在节点v10.5.0中做错了什么 [hendry@t480s learn2]$ node --experimental-modules main.mjs (node:23920) ExperimentalWarning: The ESM module loader is experimental. file:///tmp/learn2/main.mjs:1 import {hello} from 'module' // or './module' ^^^^^ Syntax
[hendry@t480s learn2]$ node --experimental-modules main.mjs
(node:23920) ExperimentalWarning: The ESM module loader is experimental.
file:///tmp/learn2/main.mjs:1
import {hello} from 'module' // or './module'
^^^^^
SyntaxError: The requested module 'module' does not provide an export named 'hello'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
[hendry@t480s learn2]$ cat main.mjs
import {hello} from 'module' // or './module'
let val = hello() // val is "Hello";
console.log('hi there')
console.log(val)
[hendry@t480s learn2]$ cat module.mjs
export function hello() {
return "Hello";
}
export default hello
从“模块”导入hello,因为您将其导出为默认值。 导出可以命名为导出(每个模块若干个)、默认导出(每个模块一个)、命名和默认的混合。 命名的导出是
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
import { square, diag } from '---';
在默认导出中,我们编写的代码类似于,export default function(){}
,当文件中只有一个导出时使用。注意,函数没有名称
//------ myFunc.js ------
export default function () { ... };
//------ main1.js ------
import myFunc from 'myFunc';
myFunc();
此处有一张备忘单:从“模块”导入
hello
,因为您将其导出为默认值。
导出可以命名为导出(每个模块若干个)、默认导出(每个模块一个)、命名和默认的混合。
命名的导出是
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
import { square, diag } from '---';
在默认导出中,我们编写的代码类似于,export default function(){}
,当文件中只有一个导出时使用。注意,函数没有名称
//------ myFunc.js ------
export default function () { ... };
//------ main1.js ------
import myFunc from 'myFunc';
myFunc();
此处的备忘单:Hello是默认导出 例如,在react中,react是默认导出,因为这通常是您需要的唯一部分。您并不总是使用组件,因此这是一个命名的导出,您可以在需要时导入
import React, {Component} from 'react`';
Hello是默认的导出 例如,在react中,react是默认导出,因为这通常是您需要的唯一部分。您并不总是使用组件,因此这是一个命名的导出,您可以在需要时导入
import React, {Component} from 'react`';
如果模块中只有一个函数,则可以执行以下操作:
export default function hello() {
return "Hello";
}
然后像这样导入它:
import hello from './module'
导入使用导出默认值导出的模块时,可以选择名称:
import greeting from './module'
您不能使用导出默认值导出const
let
和var
如果模块中有多个功能,您可以执行以下操作:
export function hello() {
return "Hello";
}
export function bye() {
return "Bye";
}
或
并通过以下方式导入函数:
import { hello } from './module'
如果模块中只有一个函数,则可以执行以下操作:
export default function hello() {
return "Hello";
}
然后像这样导入它:
import hello from './module'
导入使用导出默认值导出的模块时,可以选择名称:
import greeting from './module'
您不能使用导出默认值导出const
let
和var
如果模块中有多个功能,您可以执行以下操作:
export function hello() {
return "Hello";
}
export function bye() {
return "Bye";
}
或
并通过以下方式导入函数:
import { hello } from './module'
默认情况下,只能导出一个变量、函数或任何内容。然后,您可以在任何需要的地方导入
export default function hello(){
return "Hello";
}
或者,在文件的最后一页导出函数
function hello(){
return "Hello";
}
export default hello;
然后,您可以导入为
import hello from '../module.js'
默认情况下,只能导出一个变量、函数或任何内容。然后,您可以在任何需要的地方导入
export default function hello(){
return "Hello";
}
或者,在文件的最后一页导出函数
function hello(){
return "Hello";
}
export default hello;
然后,您可以导入为
import hello from '../module.js'
但是导出函数hello
是一个命名的导出,对吗?(我本以为模块既有默认导出,也有命名导出)但是导出函数hello
是命名导出,对吗?(我原以为该模块同时具有默认导出和命名导出)您是否尝试过从“/module”
中导出?您是否尝试过从“/module”
中导出?虽然此链接可能会回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@当然可以!我会更新答案。谢谢虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@当然可以!我会更新答案。谢谢